2018青岛ICPC网络赛C题 Halting Problem(模拟)

版权声明:本文为博主原创文章,转载请附上注明就行_(:з」∠)_。 https://blog.csdn.net/vocaloid01/article/details/82772131

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.

题解:

判断是否能运行结束说白了就是判断是否会死循环,也就是判断每一步会不会出现相同的值。所以只需要模拟出程序运行过程并开个数组判断是否当前值重复了就行。

但这题也特别坑,比赛的时候错误率也非常高。因为如果用set做判定的话会爆内存,用map的话会T(真心恶心)。吃一堑长一智以后内存允许的情况下还是首选bool数组。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAXN = 10005;

struct D{
	int id,v,k;
}board[MAXN];

bool book[MAXN][256];

int main(){
	
	int T,N;
	string s;
	scanf("%d",&T);
	while(T--){
		memset(book,false,sizeof book);
		scanf("%d",&N);
		for(int i=1 ; i<=N ; ++i){
			cin >> s;
			if(s == "add"){
				board[i].id = 1;
				scanf("%d",&board[i].v);
			}
			else if(s == "beq"){
				board[i].id = 2;
				scanf("%d %d",&board[i].v,&board[i].k);
			}
			else if(s == "bne"){
				board[i].id = 3;
				scanf("%d %d",&board[i].v,&board[i].k);
			}
			else if(s == "blt"){
				board[i].id = 4;
				scanf("%d %d",&board[i].v,&board[i].k);
			}
			else if(s == "bgt"){
				board[i].id = 5;
				scanf("%d %d",&board[i].v,&board[i].k);
			}
		}
		int t = 1;
		int num = 0;
		bool flag = true;
		while(true){
			if(t == N+1)break;
			if(book[t][num]){
				flag = false;
				break;
			}
			else book[t][num] = true;
			switch(board[t].id){
				case 1:
					num = (num + board[t].v) % 256;
					++t;
					break;
				case 2:
					if(num == board[t].v)t = board[t].k;
					else ++t;
					break;
				case 3:
					if(num != board[t].v)t = board[t].k;
					else ++t;
					break;
				case 4:
					if(num < board[t].v)t = board[t].k;
					else ++t;
					break;
				case 5:
					if(num > board[t].v)t = board[t].k;
					else ++t;
					break;
			}
		}
		if(flag)printf("Yes\n");
		else printf("No\n");
	}
	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/vocaloid01/article/details/82772131