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

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.


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

const int N =1e4+;
int vis[N][];
int t,n;
struct Ma{
char s[];
int v,k;
}ma[N];
void init()
{
for(int i=;i<=n;i++)
{
for(int j=;j<;j++){//一开始写成了26
vis[i][j]=;
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(int i=;i<=n;i++)
{
scanf("%s",ma[i].s);
if(ma[i].s[]=='a') {
scanf("%d",&ma[i].v);
ma[i].k=;
}
else{
scanf("%d%d",&ma[i].v,&ma[i].k);
}
}
int pos=,ret=,flag=;
while()
{
if(pos==n+) {
flag=;
break;
}
if(vis[pos][ret]) {
flag=;
break;
}
vis[pos][ret]++;//由ret到第pos个指令,若重复出现就会死循环
if(ma[pos].s[]=='a') {
ret=(ret+ma[pos].v)%;
pos++;
}
else if(ma[pos].s[]=='e'){
if(ret==ma[pos].v){
pos=ma[pos].k;
}
else{
pos++;
}
}
else if(ma[pos].s[]=='n'){
if(ret!=ma[pos].v){
pos=ma[pos].k;
}
else{
pos++;
}
}
else if(ma[pos].s[]=='l'){
if(ret<ma[pos].v){
pos=ma[pos].k;
}
else{
pos++;
}
}
else if(ma[pos].s[]=='g'){
if(ret>ma[pos].v){
pos=ma[pos].k;
}
else{
pos++;
}
}
}
printf("%s\n",flag==?"Yes":"No");
}
return ;
}

zoj 4049的更多相关文章

  1. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  2. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  3. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  4. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  5. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  6. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

  7. ZOJ Problem Set - 1001 A + B Problem

    ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...

  8. zoj 1788 Quad Trees

    zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...

  9. ZOJ 1958. Friends

    题目链接: ZOJ 1958. Friends 题目简介: (1)题目中的集合由 A-Z 的大写字母组成,例如 "{ABC}" 的字符串表示 A,B,C 组成的集合. (2)用运算 ...

随机推荐

  1. 记录一个修改application.properties时遇到的坑

    有一个需求是会频繁修改配置文件中的常量,为了方便就会用unzip解压war包,修改propertites中的值后重新打war 包,部署,但是发现修改的值没有起作用,,一直在纠结...后来发现其实在编译 ...

  2. 持续集成~Jenkins构建dotnetCore的项目

    上周一个大件就是dotnet core2.0发布了,伴随着.NET Standard2.0也发布了,整个微软的生态环境大好,当然也有一个BUG出来了,比如EFCore对Mysql的支持比起1.1来说, ...

  3. java 通用查询框架Querydsl 简介

    Querydsl 是一个通用的查询框架,专注于通过JavaAPI构建类型安全的SQL查询说说Querydsl的优势吧: 1. Querydsl支持代码自动完成,因为才纯Java API编写查询,因此主 ...

  4. Windows3

    windows安装后的配置 没有网络适配器, 将USB中的驱动精灵的安装程序安装在win上, 启动精灵, 提示无法连接到网络, 使用Android类型的手机中的QQ浏览器扫码下载 win会有一些开机自 ...

  5. git上传布置代码 git优势

    ftp 软件 可直接上传至服务器但不便于管理 Git上传 GitHub/码云/codinghub 登录服务器 ssh 协议登录 ssh 账户@ip地址 密码 mkdir 创建文件 workspace ...

  6. 1169 传纸条 2008年NOIP全国联赛提高组 个人博客:attack.cf

    1169 传纸条 2008年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond         题目描述 Description 小渊和小轩 ...

  7. 如何快速构建CMBD系统-glpi

    一.CMBD系统构建步骤 起初,开发这套CMBD系统是为了帮助朋友公司简化设备统计操作,以代替人工入库方式.举个例子,单位发放笔记本,或者设备更换了硬盘,都需要人工签到,手动输入统计,安装了CMBD系 ...

  8. Android tess_two Android图片文字识别

    文字识别一般都用的tesseract-ocr. GitHub:https://github.com/tesseract-ocr/tesseract 而Android对应的比较推荐的有个tess-two ...

  9. Jmeter进行接口的性能测试

    一.录制Jmeter脚本 录制Jmeter脚本有两种方法,一种是设置代理:一种则是利用badboy软件,badboy软件支持导出jmx脚本. 这里我们介绍第二种方法,利用badboy录制脚本,然后导出 ...

  10. FreeRTOS_信号量

    FreeRTOS信号量 信号量是操作系统总重要的一部分,信号量一般用来进行资源管理和任务同步,FreeRTOS中信号量又分为二值信号量.计数型信号量.互斥信号量和递归互斥信号量.不同的信号量其应用场景 ...