C Halting Problem

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 r, 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 i-th instruction.

Instruction Description
add v Add v to the register r. As r is a 8-bit register, this instruction actually calculates (r+v)mod256 and stores the result into r, i.e. r←(r+v)mod256. After that, go on to the (i+1)-th instruction.
beq v k If the value of r is equal to v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
bne v k If the value of r isn't equal to v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
blt v k If the value of r is strictly smaller than v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
bgt v k If the value of r is strictly larger than v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.

A Dream Language program consisting of n 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 (n+1)-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 T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤10​4​​), indicating the number of instructions in the following Dream Language program.

For the following n lines, the i-th line first contains a string s (s∈{“add”,“beq”,“bne”,“blt”,“bgt”}), indicating the type of the i-th instruction of the program.

  • If s equals to "add", an integer v follows (0≤v≤255), indicating the value added to the register;
  • Otherwise, two integers v and k follow (0≤v≤255, 1≤k≤n), indicating the condition value and the destination of the jump.

It's guaranteed that the sum of n of all test cases will not exceed 10​5​​.

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 r is a 8-bit register, so after four "add 1" instructions the value of r 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 r will always be even, so it's impossible for the value of r to be equal to 7, and the program will run forever.

题意

对于一个系统,有“add”,“beq”,“bne”,“blt”,“bgt”五种操作(具体操作看题目。。)输入由这五种操作组成的程序能不能停止(进行到最后一条)

思路

用二维数组vis[i][j]记录第i步操作,数值j是否出现,如果出现,则会陷入死循环,输出“No”,停止操作,直到程序进行到最后一条。

//vis数组要定义成bool型,定义成int型会TLE

AC代码

(一开始写的没有这么长,但是一直TLE,所以就加上了各种break......最大的坑点就是vis数组的类型)

#pragma GCC optimize(3)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e4+10;
using namespace std;
int a[maxn];
int d[maxn];
int x[maxn],y[maxn];
bool vis[maxn][300];
inline int read()
{
int X=0,w=1;
char c=getchar();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
return X*w;
}
//////////////
////// //
// 1-> add //
// 2-> beq //
// 3-> bne //
// 4-> blt //
// 5-> bgt //
// //
////// //
//////////////
int main(int argc, char const *argv[])
{
int t;
t=read();
// scanf("%d",&t);
int n;
while(t--)
{
// ms(d);
// ms(a);
// ms(x);
// ms(y);
ms(vis);
char str[5];
n=read();
for(int i=1;i<=n;i++)
{
scanf("%s",str);
if(strcmp(str,"add")==0)
d[i]=1;
else if(strcmp(str,"beq")==0)
d[i]=2;
else if(strcmp(str,"bne")==0)
d[i]=3;
else if(strcmp(str,"blt")==0)
d[i]=4;
else if(strcmp(str,"bgt")==0)
d[i]=5;
if(d[i]==1)
x[i]=read();
else
{
x[i]=read();
y[i]=read();
}
}
int r=0;
int i=1;
while(1)
{
if(i>n)
{
puts("Yes");
break;
}
if(d[i]==1)
{
r=(r+x[i])%256;
if(vis[i][r])
{
puts("No");
break;
}
else
vis[i][r]=1;
i++;
if(i>n)
{
puts("Yes");
break;
}
}
else if(d[i]==2)
{
if(vis[i][r])
{
puts("No");
break;
}
else
vis[i][r]=1;
if(r==x[i])
{
i=y[i];
}
else
i++;
if(i>n)
{
puts("Yes");
break;
}
}
else if(d[i]==3)
{
if(vis[i][r])
{
puts("No");
break;
}
else
vis[i][r]=1;
if(r!=x[i])
i=y[i];
else
i++;
if(i>n)
{
puts("Yes");
break;
}
}
else if(d[i]==4)
{
if(vis[i][r])
{
puts("No");
break;
}
else
vis[i][r]=1;
if(r<x[i])
i=y[i];
else
i++;
if(i>n)
{
puts("Yes");
break;
}
}
else if(d[i]==5)
{
if(vis[i][r])
{
puts("No");
break;
}
else
vis[i][r]=1;
if(r>x[i])
i=y[i];
else
i++;
if(i>n)
{
puts("Yes");
break;
}
}
if(i>n)
{
puts("Yes");
break;
}
}
}
return 0;
}

The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online -C:Halting Problem(模拟)的更多相关文章

  1. Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP

    题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...

  2. The 2018 ACM-ICPC Asia Qingdao Regional Contest(部分题解)

    摘要: 本文是The 2018 ACM-ICPC Asia Qingdao Regional Contest(青岛现场赛)的部分解题报告,给出了出题率较高的几道题的题解,希望熟悉区域赛的题型,进而对其 ...

  3. The 2018 ACM-ICPC Asia Qingdao Regional Contest

    The 2018 ACM-ICPC Asia Qingdao Regional Contest 青岛总体来说只会3题 C #include<bits/stdc++.h> using nam ...

  4. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  5. 2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred)

    2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred) easy: ACE ...

  6. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online J - Press the Button(思维)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4056 题意 有一个按钮.一个灯.一个计时器和一个计数器,每按一次按钮,计时 ...

  7. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online Solution

    A    Live Love 水. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; const i ...

  8. 2018-2019, ICPC, Asia Yokohama Regional Contest 2018 K

    传送门:https://codeforces.com/gym/102082/attachments 题解: 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ...

  9. ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...

随机推荐

  1. php 日志模块源码解析

    php日志模块设计 Monolog 是PHP的一个日志类库解析 整体介绍:monolog日志模块遵循 PSR3 的接口规范.主要有日志格式类接口(格式化日志信息),处理类接口(写日志的驱动,通过扩展写 ...

  2. Oracle多个字段如何合并成一个字段显示

    今天记录一下在oracle中多个字段如何和合并成一个字段,使用到符号“||” 1.组合查询的数据 1)组合前查询的语句 -- 组合前数据的字段 -- select A.MID CATE_ID,A.Co ...

  3. SQLServer中sql for xml path 的用法

    我们通常需要获取一个多行的某个字段拼出的字符串,我们可以使用for xml path进行处理:下面将介绍for xml path的具体用法: 创建测试表&插入测试数据 在数据库中新增测试表 C ...

  4. learning ddr tRP and tRP tRTP CL tRAS

    referce :https://blog.csdn.net/ghostyu/article/details/7728106 tRP(RAS Precharge Time): “内存行地址控制器预充电 ...

  5. ssm框架整合中的双亲容器

    SSM中Spring双亲容器的构造过程和XML加载顺序 Spring的父子容器问题和坑 Spring使用父子容器实现了很多功能,比如在Spring MVC中,展现层Bean位于一个子容器中,而业务层和 ...

  6. JAVA设计模式(一)单例模式

    单例设计模式 Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点. 核心知识点如下: (1) 将采用单例 ...

  7. 字符与字符串3——char 的大小

    字符变量占用内存的大小,也就是char类型声明的变量,这个变量占多少字节. 一字节 char c = 'A'; printf("%d,%d\n", sizeof(c),sizeof ...

  8. 十七. Python基础(17)--正则表达式

    十七. Python基础(17)--正则表达式 1 ● 正则表达式 定义: Regular expressions are sets of symbols that you can use to cr ...

  9. 循环神经网络-极其详细的推导BPTT

    首先明确一下,本文需要对RNN有一定的了解,而且本文只针对标准的网络结构,旨在彻底搞清楚反向传播和BPTT. 反向传播形象描述 什么是反向传播?传播的是什么?传播的是误差,根据误差进行调整. 举个例子 ...

  10. NioEventLoop(netty 4.1)

    里面有个excecutor属性, 在loopgroup实例化loop的时候, 如果execute一个runnable的task的时候,检测loop启动了没有,没启动的话,执行excecutor的exe ...