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.
To do this, you must find each [latex]\left( x1, y1 \right)[/latex] coordinates containing a node, and display the [latex]\left(x2, y2\right)[/latex] coordinates of the next node to the right, and the [latex]\left(x3, y3\right)[/latex] coordinates of the next node to the bottom within the grid.
If neighbor does not exist, you must output the coordinates [latex]\left(-1, -1\right)[/latex] instead of [latex]\left(x2, y2\right)[/latex] and/or [latex]\left(x3, y3\right)[/latex].
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.
The program must first read the initialization data from standard input. Then, provide to the standard output one line per instruction.
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.[latex]0 <[/latex] width[latex]\le 30[/latex]
[latex]0 <[/latex] height[latex]\le 30[/latex]
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.
[latex]0 \le[/latex] y2[latex]<[/latex] height
[latex]-1 \le[/latex] x2, x3[latex]<[/latex] width
[latex]-1 \le[/latex] y2, y3[latex]<[/latex] height
Alloted response time to first output line [latex]\le 1[/latex]s.
Response time between two output lines [latex]\le 100[/latex]ms.
|0 0 1 0 0 1
1 0 -1 -1 -1 -1
0 1 -1 -1 -1 -1
|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
The code of the program
using namespace std;
short width; // the number of cells on the X axis
cin >> width; cin.ignore();
short height; // the number of cells on the Y axis
cin >> height; cin.ignore();
string *node = new string[height];
short x, y;
for (y = 0; y < height; ++y)
for (y = 0; y < height; ++y)
for (x = 0; x < width; ++x)
if (node[y][x] == '0')
cout << x << ' ' << y << ' ';
short x2, y2, ansX = -1, ansY = -1;
for (x2 = x+1; x2 < width; ++x2)
if (node[y][x2] == '0')
ansX = x2;
ansY = y;
cout << ansX << ' ' << ansY << ' ';
ansX = -1; ansY = -1;
for (y2 = y+1; y2 < height; ++y2)
if (node[y2][x] == '0')
ansX = x;
ansY = y2;
cout << ansX << ' ' << ansY << '\n';
x = x2-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 [latex]x[/latex] 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.