当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

1768:Hang or not to hang

题目描述
Little Tom is learning how to program. He has just written some programs but is afraid to run them, because he does not know if they will ever stop. Please write a program to help him. This task is not as easy as it may seem, because Tom's programs are possibly not deterministic. Given a program written by Tom, your program should tell him whether his program can stop and if so, what is the shortest possible time before it stops.

Tom's computer consists of 32 1-bit registers and the program consists of n instructions. The registers are numbered from 0 to 31 and the instructions are numbered from 0 to n-1.

Below, MEM[a] stands for the contents of the a-th register, 0 <= a, b < 32, 0 <= x < n, 0 <= c <= 1.

The instruction set is as follows:

InstructionSemantics

AND a b
OR a b
XOR a b
NOT a
MOV a b
SET a c
RANDOM a
JMP x
JZ x a
STOP

MEM[a] := MEM[a] and MEM[b]
MEM[a] := MEM[a] or MEM[b]
MEM[a] := MEM[a] xor MEM[b]
MEM[a] := not MEM[a]
MEM[a] := MEM[b]
MEM[a] := c
MEM[a] := random value (0 or 1)
jump to the instruction with the number x
jump to the instruction with the number x if MEM[a] = 0
stop the program

The last instruction of a program is always STOP (although there can be more than one STOP instruction). Every program starts with the instruction number 0. Before the start, the contents of the registers can be arbitrary values. Each instruction (including STOP) takes 1 processor cycle to execute.
Task
Write a program that:

reads the program,
computes the shortest possible running time of the program,
writes the result.
输入解释
The first line of the input contains an integer n (1 <= n <= 16) being the number of instructions of the program. Each of the next n lines contains one instruction of the program in the format given above. You may assume that the only white characters in the program are single spaces between successive tokens of each instruction.
输出解释
The first and only line of the output should contain the shortest possible running time of the program, measured in processor cycles. If the program cannot stop, output should contain the word HANGS.
输入样例
5
SET 0 1
JZ 4 0
RANDOM 0
JMP 1
STOP
输出样例
6

该题目是Virtual Judge题目,来自 北京大学POJ

题目来源 Central Europe 2003

源链接: POJ-1768

最后修改于 2020-10-29T06:13:30+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 10000