博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj 4049
阅读量:5126 次
发布时间:2019-06-13

本文共 4964 字,大约阅读时间需要 16 分钟。

Halting Problem

Time Limit: 1 Second      
Memory Limit: 65536 KB

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

42add 1blt 5 13add 252add 1bgt 252 22add 2bne 7 13add 1bne 252 1beq 252 1

Sample Output

YesYesNoNo

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.


Author: WENG, Caizhi

Source: The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

 

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++; } } } printf("%s\n",flag==1?"Yes":"No"); } return 0;}

 

转载于:https://www.cnblogs.com/tingtin/p/9665803.html

你可能感兴趣的文章
Delphi DBGrid 导出csv
查看>>
JVM第五部分 高效并发
查看>>
python:浅析python 中__name__ = '__main__' 的作用(转载)
查看>>
《linux就该这么学》第六节,计划任务和用户身份管理!
查看>>
php: +1天, +3个月, strtotime(): +1 day, +3 month
查看>>
mysql: 模糊查询 feild like keyword or feild like keyword , concat(feild1,feild2,feild3) like keyword...
查看>>
Python小数据池
查看>>
JavaScript 函数(作用域以及闭包)
查看>>
ORACLE EBS 多账套总结
查看>>
使用MyBatista----上传图像
查看>>
神经网络中使用Batch Normalization 解决梯度问题
查看>>
Win8 IIS 安装和部署网站问题
查看>>
GIT的Push和Pull,强制Pull覆盖本地命令
查看>>
phpMyAdmin导入大的sql文件
查看>>
Winform开发之ADO.NET对象Connection、Command、DataReader、DataAdapter、DataSet和DataTable简介...
查看>>
Ubuntu更改鼠标灵敏度
查看>>
基于 vue2 导航栏透明渐变
查看>>
JavaScript HTML DOM元素节点常用操作接口
查看>>
利用expect实现自动化操作
查看>>
Golang tcp转发 remoteAddr错误
查看>>