There is no Spoon — Episode 1

The Goal

The game is played on a rectangular grid with a given size. Some cells contain power nodes. The rest of the cells are empty.

The goal is to find, when they exist, the horizontal and vertical neighbors of each node.

Rules

To do this, you must find each $\left( x1, y1 \right)$ coordinates containing a node, and display the $\left(x2, y2\right)$ coordinates of the next node to the right, and the $\left(x3, y3\right)$ coordinates of the next node to the bottom within the grid.

If neighbor does not exist, you must output the coordinates $\left(-1, -1\right)$ instead of $\left(x2, y2\right)$ and/or $\left(x3, y3\right)$.

You lose if:

• You give an incorrect neighbor for a node.
• You give the neighbors for an empty cell.
• You compute the same node twice.
• You forget to compute the neighbors of a node.

Game input

The program must first read the initialization data from standard input. Then, provide to the standard output one line per instruction.

Initialization input

Line 1: one integer width for the number of cells along the x axis.

Line 2: one integer height for the number of cells along the y axis.

Next height lines: A string line containing width characters. A dot . represents an empty cell. A zero 0 represents a cell containing a node.

$0 <$ width$\le 30$
$0 <$ height$\le 30$

Output for one game turn

One line per node. Six integers on each line: x1 y1 x2 y2 x3 y3
Where:

• ( x1, y1) the coordinates of a node.
• ( x2, y2) the coordinates the closest neighbor on the right of the node.
• ( x3, y3) the coordinates the closest bottom neighbor.

$0 \le$ x1$<$ width
$0 \le$ y2$<$ height
$-1 \le$ x2, x3$<$ width
$-1 \le$ y2, y3$<$ height
Alloted response time to first output line $\le 1$s.
Response time between two output lines $\le 100$ms.

Input Output
2 2
00
0.
0 0 1 0 0 1
1 0 -1 -1 -1 -1
0 1 -1 -1 -1 -1
4 4
.0..
.000
000.
..0.
1 0 -1 -1 1 1
1 1 2 1 1 2
2 1 3 1 2 2
3 1 -1 -1 -1 -1
0 2 1 2 -1 -1
1 2 2 2 -1 -1
2 2 -1 -1 2 3
2 3 -1 -1 -1 -1

Solution of the task

First of all, we must pay attention, that we have to find the closest neighbor. It doesn’t mean, that if there is no neighbor on adjacent cells, then the answer will be negative, because the neighbor may be further. This leads to the fact, that the task can not be completed without memorization of the whole list of cells.

After storing every string in array, the task becomes simple: we go using the cycle through every cell, and if the cell contains a node, then we launch two cycles from it in two directions (to the right and to the bottom), and assume there are no neighbors with assigning value -1 to both variables ansX and ansY. If there will be no nodes found, the value will remain the same, otherwise variables will take values of the node coordinates. In any case, the result will be correct.

This process is optimized by usage of the following: the $x$ coordinate of the closest right neighbor (or the value of width) is saved in a variable x2. Whether we find a neighbor or no, we can start the further horizontal search right from the coordinate x2, because empty cells must be skipped anyway.