題目如下:
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.
Instruction | Description |
---|---|
add | Add to the register . As is a 8-bit register, this instruction actually calculates and stores the result into , i.e. . After that, go on to the -th instruction. |
beq | If the value of is equal to , jump to the -th instruction, otherwise go on to the -th instruction. |
bne | If the value of isn't equal to , jump to the -th instruction, otherwise go on to the -th instruction. |
blt | If the value of is strictly smaller than , jump to the -th instruction, otherwise go on to the -th instruction. |
bgt | If the value of is strictly larger than , jump to the -th instruction, otherwise go on to 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.
Input
There 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.
-
If equals to "add", an integer follows (), indicating the value added to the register;
-
Otherwise, two integers and follow (, ), indicating the condition value and the destination of the jump.
It's guaranteed that the sum of of all test cases will not exceed .
Output
For 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 Input
4 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 Output
Yes Yes No No
Hint
For 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.
題記:
1.問題重述:“Halting Problem”是圖靈停機問題,意思是執行表中的五種操作,若能停止則輸出“Yes”,若程序會永遠運行下去沒有終止那一刻,則輸出“No”。
2.問題分析:判斷是否終止運行有兩個條件:①共n步操作,需要進入n+1步操作,n+1步不存在,則停止運行。②每一步執行超過256次則視爲改程序陷入了死循環,永遠不會停止運行。
2.解題思路:①用instruction結構體存儲輸入的操作。②用step記錄當前進行的操作是第幾個,模擬程序運行過程。③用count[]數組記錄每一步操作執行的次數,用於判斷程序是否陷入了死循環。(詳細步驟見代碼中的註釋)
C++代碼如下:
#include <iostream>
#include <cstring>
using namespace std;
struct instruction{
string operation;
int v, k;
};
int main(int argc, char** argv) {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
instruction s[n+2];
//輸入
for(int i=1; i<=n; i++){
//讀入操作
cin >> s[i].operation;
//如果是add操作就讀入v,否則讀入v、k
if(s[i].operation[0] == 'a'){
cin >> s[i].v;
}else{
cin >> s[i].v >> s[i].k;
}
}
/*//調試
for(int i=1; i<=n; i++){
cout << "i:" << i << " " << s[i].operation << " " << s[i].v << " " << s[i].k <<endl;
} */
//模擬
int count[n+1] = {0}; //計數,256步之後還不停止則輸出no
int step = 1; //記錄當前操作步驟;
int r = 0;
int flag = 0; //記錄是否結束
while(1){
count[step]++;
//如果當前操作是add……
if(s[step].operation == "add"){
r = (r + s[step].v) % 256;
step++;
}
//如果當前操作是beq……
else if(s[step].operation == "beq"){
if(r == s[step].v){
step = s[step].k;
}else{
step++;
}
}
//如果當前操作是bne……
else if(s[step].operation == "bne"){
if(r != s[step].v){
step = s[step].k;
}else{
step++;
}
}
//如果當前操作是blt……
else if(s[step].operation == "blt"){
if(r < s[step].v){
step = s[step].k;
}else{
step++;
}
}
//如果當前操作是bgt……
else if(s[step].operation == "bgt"){
if(r > s[step].v){
step = s[step].k;
}else{
step++;
}
}
//判斷結束
if(step > n){
flag = 1;
break;
}
if(count[step] >= 256){
break;
}
}
//輸出
if(flag){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
}
return 0;
}