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. Maven--setting详解

    settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. settings.xml文件是干什么的,为什么要配置它呢? ...

  2. (转)linux traceroute命令参数及用法详解--linux跟踪路由命令

    linux traceroute命令参数及用法详解--linux跟踪路由命令 原文:http://blog.csdn.net/liyuan_669/article/details/25362505 通 ...

  3. ms sqlserver 清除数据库日志脚本

    USE [master] GO ALTER DATABASE F360DW SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE F360DW SET ...

  4. ssas 为绑定指定的大小太小,导致一个或多个列值被截断

    错误信息:ssas 为绑定指定的大小太小,导致一个或多个列值被截断 如果更改了某个维度或是事实表的字段长度,在处理CUBE时提示此错误,我们要做以下更新: 1.刷新数据源视图. 2.打开多维数据集,查 ...

  5. Smarty中的请求变量和保留变量的使用范例

    PHP中提供的超全局数组 Smarty中对应的请求变量 $_GET               <{$smarty.get}> $_POST                         ...

  6. SQL2000,2005,2008安装在一台机子上

    工欲善其事,必先利其器.本机的系统是在网上自己下载的,是32位windows7旗舰版.因为学习,需要在一台机子上同时安装SQL Server2000,2005,2008三个版本的数据库.先是在网上查了 ...

  7. Java中的构造函数——通过示例学习Java编程(14)

      作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=25 构造函数是用来初始化新创建的对象的代码块. ...

  8. 关于下载文件封装的两个类(Mars)

    首先是文件FileUtils.java package mars.utils; import java.io.File; import java.io.FileOutputStream; import ...

  9. 【持续更新】Java 时间相关

    直接上代码: import java.util.*; import java.text.SimpleDateFormat; public class HelloWorld { public stati ...

  10. java类及编写public类的基础点

    1.一个java文件中只能有一个public类.且公共类名称必须与java文件名一致,否则会出现错误提示.与其他面向对象编程语言的一样,在利用java分析问题时,基本思路即为将问题的属性(静)与行为( ...