Langton's Ant#787

Langton's Ant

2 seconds
64 megabytes
Medium

Langton's ant, named after mathematician Christopher Langton, is a cellular automaton with simple rules but interesting emergent behavior. Some mathematicians study this behavior. The analogy between ants and Langton's automaton comes from imagining the automaton's state as an "ant" and its movement as the ant traveling through a special world.

The ant's world is an n \times n plane where each cell is colored blue or red. The number is the size of the world. Each cell is denoted by , where 1 \leq i, j \leq n. The ant moves step by step according to the following rules:

For example, if the ant is on a red cell facing east and there is no cell immediately to its south, the ant dies. If there is a cell to its south, the ant moves there, faces south, and the original cell becomes blue. Then, the ant tries to make another step, and so on.

Your task: Given (i) the configuration of the world and (ii) the initial position of the ant, determine whether the ant can reach the cell. The ant's initial direction is north.

Input

The configuration of an n \times n world can be represented as a non-negative integer, using n^2 bits in binary notation. We use the following conventions: for blue, for red, and the binary representation identifies the cells from left to right, bottom to top (most significant bit to least significant bit). For example, ( in decimal) represents a 2 \times 2 world with the following configuration:

Similarly, ( in decimal) represents a 3 \times 3 world with the configuration:

The input contains multiple test cases. Each case is a line with four integers: , , , , separated by spaces. They mean:

  • (1 \leq n \leq 16): The world size.

  • (0 \leq c < 2^{n^2}): Decimal representation of the n^2-bit configuration.

  • : Initial coordinates of the ant (1 \leq x, y \leq n), where corresponds to the least significant bit of .

End of input: n = c = x = y = 0.

Output

For each test case, output:

  • Yes if the ant reaches cell from the initial position.

  • Kaputt! if it dies before reaching cell .

It is guaranteed that for each test case, after a finite number of steps, the ant will either reach or die before reaching it.

Examples

Example 1

Loading
Loading