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)用运算 ...
随机推荐
- 如何安装使用windows自带的telnet服务
控制面板->程序和功能->打开或关闭Windows功能->Telnet 客户端 [ Telnet 服务器 ] 安装完成后重启cmd telnet ip port
- MySQL 实现字符串换行
target_describe字段值中包含 :[ 这两个特殊的字符 ,想要在字符之间加换行 需要插入CHAR(10) ),'[')) UPDATE ew_pm_project_red_detail S ...
- jstl表达式的应用的条件
在el表达式中,有时我们要写for循环,这时我们要写 <c:forEach items="${list}" var="news" > </c: ...
- nmap扫描开放端口
nmap 192.168.1.1 -p1-65535 指定端口范围使用-p参数,如果不指定要扫描的端口,Nmap默认扫描从1到1024再加上nmap-services列出的端口 nmap-servi ...
- 【Unity3D】资源对象、预设、查找对象、组合模式等知识点
1.解释对象与资源区别于联系,根据官方案例,分别总结资源和对象组织的规则/规律. 下载并查看了Adam和Survival Shooter tutorial两个官方资源案例,这些案例作为资源,可以 ...
- .Net平台互操作技术:03. 技术验证
上面两篇文章分别介绍了.Net平台互操作技术面临的问题,并重点介绍了通过P/Invoke调用Native C++类库的技术实现.光说不做是假把式,本文笔者将设计实验来证明P/Invoke调用技术的可行 ...
- 利用C#脚本来处理Excel
废不多,直入正题. 所需环境:安装了Windows操作系统和Office软件的电脑一台. 开发语言:C# 开发需求:1.利用C#脚本读取Excel .xlsx文件 2.将程序中的数据存储到.csv文件 ...
- CAS 配置NLB 负载均衡网络无法连接
在虚拟机与虚拟机.虚拟机与实机之间利用Windows操作系统自带的网络负载均衡功能如选择单播集群模式,网络就无法通讯,NLB不成功. Scenario #1 在虚拟机与虚拟机之间选择多播模式NLB可正 ...
- 使用github进行代码托管
---恢复内容开始--- 记录下使用github进行个人代码托管,github是公共的代码托管库,可以免费使用,由于是公共的所以大家都可以查看,如果是隐私重要的文件代码可以选择付费变为私有库 1.注册 ...
- python爬虫之路——初识数据库存储
非关系型数据库:MongoDB.关系型数据库:MySQL 关系型和非关系型的区别: 安装: 使用: 应用场景: mongoDB是一种非关系型数据库,分为四大类:键值存储数据库,列存储数据库,文档型数据 ...