zoj 4049
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的更多相关文章
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
- ZOJ 1958. Friends
题目链接: ZOJ 1958. Friends 题目简介: (1)题目中的集合由 A-Z 的大写字母组成,例如 "{ABC}" 的字符串表示 A,B,C 组成的集合. (2)用运算 ...
随机推荐
- CS round--36
https://csacademy.com/contest/round-36/summary/ C题是一个贪心,最坏情况是,一开始肯定是每一对袜子都抽一个,然后就需要N个袜子了.后面的情况就是相同的了 ...
- 为什么数据库ID不能作为URL中的标识符
最近公司在进行网站的SEO优化,将所有主要页面的URL统一更改为新的格式,其中重要的一项改变是将所有URL的标识符统一为ID,例如过去我们的一个用户的公共页面URL是这样的 https://www.e ...
- 关于Memcache的连接
addServer 在说Memcache的长连接(pconnect)和短连接(connect)之前要先说说Memcache的addServer,Memcache的addServer是增加一个服务器到连 ...
- Unity注入
[此文引用别人,作为随笔自己看.]今天写<WCF技术剖析(卷2)>关于<WCF扩展>一章,举了“如何通过WCF扩展实现与IoC框架(以Unity为例)集成”(<通过自定义 ...
- 快色排序算法(C语言描述)
快速排序 算法思想 快速排序采用了一种分治策略,学术上称之为分治法(Divide-and-Conquer Method). 哨兵(如下算法中的key) 每趟排序将哨兵插入到数组的合适位置,使得哨兵左侧 ...
- socket tcp使用recv接收数据时,返回errno错误代码88
原因:就是recv函数的第一个参数不是可用的,也就是第一个参数不是建立连接时返回的文件描述符. 解决方法:xxx
- LR脚本示例之常用函数
1.变量和参数的设置 //将IP地址和端口放入到参数中lr_save_string("127.0.0.1:1080","ip"); //退出脚本建议使用lr_e ...
- 微软分布式机器学习工具包DMTK——初窥门径
在现在机器学习如日中天的大背景下,微软亚洲研究院的实习岗位中,机器学习组的工作也是维护DMTK,参与算法改进,那么在此之前我们得了解DMTK是个啥. DMTK由一个服务于分布式机器学习的框架和一组分布 ...
- QT 调试输出格式
Qt调试输出格式: 1,qDebug() << qPrintable(firstNode.nodeName()) << qPrintable(firstNode.nodeVal ...
- VA助手添加扩展文件后缀名
Allow C/C++ files with a non-standard extension Follow these steps to make Visual Assist consider as ...