zoukankan      html  css  js  c++  java
  • 2018 ACM-ICPC 亚洲青岛区域网络赛 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.

    作者: 浙江大学竞赛命题组

    单位: ACMICPC

    时间限制: 500 ms

    内存限制: 64 MB

    代码

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct point{
    	string s;
    	int k,v;
    };
    int n;
    point a[10010];
    int vis[10010];
    int main(){
    	int t;
    	cin>>t;
    	while(t--){
    		cin>>n;
    		for(int i=1;i<=n;i++){
    			cin>>a[i].s;
    			if(a[i].s=="add") cin>>a[i].v;
    			else cin>>a[i].v>>a[i].k;
    		}
    		int r=0;
    		int j=1;
    		int sum=0;
    		memset(vis,0,sizeof(vis));
    		while(1){
    			if(j>=n+1) break;
    			if(vis[j]>256) break;
    			if(a[j].s=="add"){
    				r=(r+a[j].v)%256;
    				vis[j]++;
    				j++;
    			}
    			if(j>=n+1) break;
    			if(vis[j]>256) break;
    			if(a[j].s=="beq"){
    				vis[j]++;
    				if(r==a[j].v) j=a[j].k;
    				else j++;
    			}
    			if(j>=n+1) break;
    			if(vis[j]>256) break;
    			if(a[j].s=="bne"){
    				vis[j]++;
    				if(r!=a[j].v) j=a[j].k;
    				else j++;
    			}
    			if(j>=n+1) break;
    			if(vis[j]>256) break;
    			if(a[j].s=="blt"){
    				vis[j]++;
    				if(r<a[j].v) j=a[j].k;
    				else j++;
    			}
    			if(j>=n+1) break;
    			if(vis[j]>256) break;
    			if(a[j].s=="bgt"){
    				vis[j]++;
    				if(r>a[j].v) j=a[j].k;
    				else j++;
    			}
    			if(j>=n+1) break;
    			if(vis[j]>256) break;
    		}
    		if(j>=n+1) cout<<"Yes"<<endl;
    		else cout<<"No"<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    .Net/C#: 实现支持断点续传多线程下载
    软件设计过程中常用的几种图(二)
    走马灯 代码appendChild实现的无缝滚动
    调用web service时的SOAPHEADER验证
    Excel VBA 学习总结 数据验证与正则表达式
    C#者重建C++之路 运行机制的差异
    Excel VBA 学习总结 代码优化之道
    Excel VBA 学习总结 文件系统
    软件设计:“度”、“裁剪”与“变通”
    Excel VBA 学习总结 开发模式
  • 原文地址:https://www.cnblogs.com/Romantic-Chopin/p/12451410.html
Copyright © 2011-2022 走看看