Given a program of labeled print statements and if-goto statements with counters that are true for a bounded number of times, decide whether transforming every if-goto into a do-while loop keeps the program's output and compiles.
Hard8SimulationImplementationGraphDFSNo attempts yetTime limit2sMemory limit256 MBEdsger Wybe Dijkstra, a Dutch computer scientist and a Turing award winner, has a name that is hard to pronounce. In the 1960s he wrote the following.
For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the density of go to statements in the programs they produce. More recently I discovered why the use of the go to statement has such disastrous effects, and I became convinced that the go to statement should be abolished from all "higher level" programming languages.
You do not want to produce low quality code. Your source code contains no loop statements, and the only flow control statement in it is the if-goto. To decrease the density, you have to eliminate every if-goto statement in your source code, written in a C-like language, and replace it with a do-while loop in the following manner.
if (boolean_expression) goto some_label;.do { right after where some_label is declared.if by } while.goto some_label.For example, the following code
int main() {
int score;
get_score:
scanf("%d",&score);
if (score < 0 || score > 100) goto get_score;
if (score < 60) goto fail;
fail:
puts("you are failed!");
return 0;
}
is modified into
int main() {
int score;
get_score: do {
scanf("%d",&score);
} while (score < 0 || score > 100) ;
} while (score < 60) ;
fail: do {
puts("you are failed!");
return 0;
}
It is not too surprising that the code above cannot be compiled. Here is your task: given a sequence of statements, determine whether all if-goto statements can be replaced by do-while loops without changing the output of the program. For simplicity, you may assume all statements are in one of the following two forms.
line_x: puts("x"); where x is the line number of this statement and puts("x") prints the line number x.if (expr_x()) goto line_y; where x is the line number of this statement and line_y is a valid label in the program. expr_x() returns true on its first x invocations and false afterward.If the modification makes the code unable to compile, the output of the program counts as different from the original output.
The first line contains a single integer T, the number of test cases. (T≤20)
Each test case is a sequence of statements. Each statement is on a single line, and line numbers start at 1. A line is a statement of form 1, a statement of form 2, or END. END marks the end of a test case. It can only be the last line of a test case, and it is not part of the program. A single test case has at most 10000 statements (END is not counted).
For each test case, print good on a single line if replacing all if-goto statements with do-while loops does not change the output of the program. Otherwise print bad. If the modified code no longer compiles, print bad.