Halting Problem

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 r, 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 i-th instruction.

InstructionDescription
add vAdd v to the register r. As r is a 8-bit register, this instruction actually calculates (r+v)mod256 and stores the result into r, i.e. r←(r+v)mod256. After that, go on to the (i+1)-th instruction.
beq v kIf the value of r is equal to v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
bne v kIf the value of r isn't equal to v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
blt v kIf the value of r is strictly smaller than v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
bgt v kIf the value of r is strictly larger than v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.

A Dream Language program consisting of n 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 (n+1)-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 T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤10​4​​), indicating the number of instructions in the following Dream Language program.

For the following n lines, the i-th line first contains a string s (s∈{“add”,“beq”,“bne”,“blt”,“bgt”}), indicating the type of the i-th instruction of the program.

  • If s equals to "add", an integer v follows (0≤v≤255), indicating the value added to the register;
  • Otherwise, two integers v and k follow (0≤v≤255, 1≤k≤n), indicating the condition value and the destination of the jump.

It's guaranteed that the sum of n of all test cases will not exceed 10​5​​.

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

 

 

思路

就是模拟啦,注意扣常数,直接用二维数组标记就行了,(之前用vector,map,set)一直超时或超内存。

AC代码

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cstdio>
#include <vector>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define N 1000005
#define newmax(a,b) a>b?a:b
#define newmin(a,b) a>b?b:a
#define Lowbit(x) (x&-x)
using namespace std;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };

int num;
struct instruction
{
    char s[10];
    int v;
    int k;
}s[10100];


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        bool v[10010][260]={0};
        scanf("%d",&num);
        int r=0;
        for(int i=1;i<=num;i++)
        {
            scanf(" %s",s[i].s);
            if(s[i].s[1]=='d')
            {
                scanf(" %d",&s[i].v);
            }
            else{
                scanf(" %d%d",&s[i].v,&s[i].k);
            }
        }
        int p=1;
        while(1)
        {
            if(p>num)
                break;
            if(s[p].s[1]=='d'){
                if(v[p][r]==1)
                    break;
                else
                    v[p][r]=1;
                r=(r+s[p].v)%256;
                p++;
                continue;
            }
            else if(s[p].s[1]=='e'){
                if(v[p][r]==1)
                    break;
                else
                    v[p][r]=1;
                if(r==s[p].v)
                    p=s[p].k;
                else
                    p++;
                continue;
            }
            else if(s[p].s[1]=='n'){
                if(v[p][r]==1)
                    break;
                else
                    v[p][r]=1;
                if(r!=s[p].v)
                    p=s[p].k;
                else
                    p++;
                continue;
            }
            else if(s[p].s[1]=='l'){
                if(v[p][r]==1)
                    break;
                else
                    v[p][r]=1;
                if(r<s[p].v)
                    p=s[p].k;
                else
                    p++;
                continue;
            }
            else if(s[p].s[1]=='g'){
                if(v[p][r]==1)
                    break;
                else
                    v[p][r]=1;
                if(r>s[p].v)
                    p=s[p].k;
                else
                    p++;
                continue;
            }
        }
        if(p>num)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值