zoj 4049
In computability theory,the halting problem is the problem of determining,from a description of an arbitrary computer program,whether the program will finish running (i.e.,halt) or continue to run forever. Alan Turing proved in 1936 that a general algorithm to solve the halting problem cannot exist,but DreamGrid,our beloved algorithm scientist,declares that he has just found a solution to the halting problem in a specific programming language -- the Dream Language! Dream Language is a programming language consists of only 5 types of instructions. All these instructions will read from or write to a 8-bit register?,whose value is initially set to 0. We now present the 5 types of instructions in the following table. Note that we denote the current instruction as the?-th instruction.
A Dream Language program consisting of??instructions will always start executing from the 1st instruction,and will only halt (that is to say,stop executing) when the program tries to go on to the?-th instruction. As DreamGrid‘s assistant,in order to help him win the Turing Award,you are asked to write a program to determine whether a given Dream Language program will eventually halt or not. InputThere are multiple test cases. The first line of the input is an integer?,indicating the number of test cases. For each test case: The first line contains an integer??(),indicating the number of instructions in the following Dream Language program. For the following??lines,the?-th line first contains a string??(),indicating the type of the?-th instruction of the program.
It‘s guaranteed that the sum of??of all test cases will not exceed?. OutputFor each test case output one line. If the program will eventually halt,output "Yes" (without quotes); If the program will continue to run forever,output "No" (without quotes). Sample Input4 2 add 1 blt 5 1 3 add 252 add 1 bgt 252 2 2 add 2 bne 7 1 3 add 1 bne 252 1 beq 252 1 Sample OutputYes Yes No No HintFor the second sample test case,note that??is a 8-bit register,so after four "add 1" instructions the value of??will change from 252 to 0,and the program will halt. For the third sample test case,it‘s easy to discover that the value of??will always be even,so it‘s impossible for the value of??to be equal to 7,and the program will run forever. Author:?WENG,Caizhi ? const int N =1e4+5; int vis[N][260]; int t,n; struct Ma{ char s[10]; int v,k; }ma[N]; void init() { for(int i=1;i<=n;i++) { for(int j=0;j<260;j++){//一开始写成了26 vis[i][j]=0; } } } int main() { scanf("%d",&t); while(t--) { scanf("%d",&n); init(); for(int i=1;i<=n;i++) { scanf("%s",ma[i].s); if(ma[i].s[0]==‘a‘) { scanf("%d",&ma[i].v); ma[i].k=0; } else{ scanf("%d%d",&ma[i].v,&ma[i].k); } } int pos=1,ret=0,flag=0; while(1) { if(pos==n+1) { flag=1; break; } if(vis[pos][ret]) { flag=0; break; } vis[pos][ret]++;//由ret到第pos个指令,若重复出现就会死循环 if(ma[pos].s[0]==‘a‘) { ret=(ret+ma[pos].v)%256; pos++; } else if(ma[pos].s[1]==‘e‘){ if(ret==ma[pos].v){ pos=ma[pos].k; } else{ pos++; } } else if(ma[pos].s[1]==‘n‘){ if(ret!=ma[pos].v){ pos=ma[pos].k; } else{ pos++; } } else if(ma[pos].s[1]==‘l‘){ if(ret<ma[pos].v){ pos=ma[pos].k; } else{ pos++; } } else if(ma[pos].s[1]==‘g‘){ if(ret>ma[pos].v){ pos=ma[pos].k; } else{ pos++; } } } printf("%sn",flag==1?"Yes":"No"); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |