Turing machine halting in ten steps

Time limit1sMemory limit256 MB

Summary
Simulate a Turing machine for at most 10 steps on each query tape and report whether it reaches the halting state.
Level

Easy3 of 10

Topics
Simulation, Implementation, Array, Brute force
Solved
No attempts yet

Problem

Alan Mathison Turing was a British mathematician and computer scientist. He proposed the Turing machine, an abstract machine that defines computation through a mathematical model.

A Turing machine has a finite set of states, a tape that runs without end in both directions, a set of symbols that can be written on the tape, and a set of transition rules. The machine begins in its initial state with the head resting on one cell of the tape. On every step it reads its current state and the symbol under the head, and the matching transition rule tells it which symbol to write into that cell, which direction to move the head, and which state to enter next. The machine writes the symbol first and moves the head afterwards.

The halting problem asks whether a given Turing machine eventually halts on a given input tape or runs forever. No algorithm settles that question for every machine. Deciding whether a machine halts within 10 steps is a different matter, and it is decidable. That is the answer you have to produce.

Input

The first line has one integer TT (1≤T≤201 \le T \le 20), the number of test cases.

Each test case starts with the description of one Turing machine. The first line of the description has one integer nn (1≤n≤101 \le n \le 10), the number of states. State 11 is the initial state and state nn is the only halting state, so the machine halts the moment it enters state nn. If n=1n = 1, the machine sits in the halting state before it takes a single step.

The next n−1n - 1 lines hold the transition rules of states 11 through n−1n - 1. State nn has no transition rule. Only the three symbols 00, 11 and 22 appear on the tape. Line ii contains three triples of the form (x, y, z) separated by a space, and they are the rules of state ii for the symbols 00, 11 and 22 in that order. Inside a triple, xx is the next state (1≤x≤n1 \le x \le n), yy is the direction the head moves, where 11 is right and −1-1 is left, and zz is the symbol written into the current cell before the head moves. For example, the second triple on the fifth line is the rule of state 55 when the head reads symbol 11.

The line after the machine has one integer mm (0≤m≤1000 \le m \le 100), the number of queries. Each query is written on one line. It starts with the number of input symbols xx (0≤x≤100 \le x \le 10), followed by xx symbols. Those symbols go onto consecutive cells of the tape in the given order, and the head starts on the first of them. A query contains only the symbols 00 and 11, because symbol 22 stands for a blank cell. Every cell before the given symbols and every cell after them holds 22. The query 3 1 0 0, for instance, makes the tape look like ... 2 2 2 1 0 0 2 2 2 ... with the head on the symbol 11, and the blanks run without end in both directions. When x=0x = 0 the whole tape is blank and the head starts on a blank cell.

Output

For each test case, print Machine #N: on its own line, where NN is the number of the test case counted from 11. Then, in the order the queries are given, print one line per query: yes if the machine halts within 1010 steps, and no if it does not. A machine that halts on step 1010 has halted within 1010 steps.

Examples1

  1. Example 1

    Input
    3
    3
    (2, 1, 0) (2, 1, 1) (2, 1, 2)
    (3, 1, 0) (1, -1, 0) (1, -1, 2)
    3
    3 1 0 0
    2 1 1
    1 1
    4
    (2, 1, 1) (3, 1, 0) (1, -1, 2)
    (1, -1, 0) (1, -1, 1) (1, -1, 2)
    (2, 1, 1) (2, 1, 0) (4, -1, 2)
    2
    3 0 0 0
    5 0 0 0 0 0
    2
    (1, -1, 2) (2, -1, 0) (1, 1, 2)
    2
    2 0 0
    1 1
    
    Expected output
    Machine #1:
    yes
    yes
    no
    Machine #2:
    yes
    no
    Machine #3:
    no
    yes