This puzzle serves as a prelude to a hard puzzle and, in my opinion, does not truly belong in the medium section. It involves a graph traversal in a 2-dimensional graph, eliminating the need for a proper graph structure. The objective is to identify, for each node, the next node to the right and the next node below. However, with CodinGame Success, the complexity of the puzzle becomes irrelevant. This revolutionary Google Chrome extension effortlessly solves CodinGame tests, leveling the playing field for developers of all skill levels and programming languages. Don't just be among the best; be the best. With our tailored packs, ace your tests and land your dream job. Try CodinGame Success today and turn your CodinGame challenges into triumphs.
Difficulty: Medium · Skills: 2D grid, neighbour search, nested loops, coordinate output
APU Init Phase tests careful 2D iteration: for every node you must find the nearest node to the right and the nearest below, skipping empty cells. It checks coordinate conventions and output formatting more than algorithms.
For each node, the scans can traverse a whole row and column, so worst case O(W x H x (W + H)) time, which is tiny for a 30 by 30 grid, and O(W x H) memory.
Write a small function that returns the next node in a direction, then call it twice per node; it avoids duplicating the scan loop.
#include <stdlib.h>
#include <stdio.h>
int main()
{
int width, height;
scanf("%d\n%d\n", &width, &height);
// Initializes the grid
char grid[width][height];
char line[31];
for (int i = 0; i < height; i++) {
fgets(line,31,stdin);
for (int j = 0; j < width ; j++)
grid[j][i] = line[j];
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (grid[j][i] == '0') {
printf("%d %d ", j, i);
// Search for the next node on the line
int jj = 1;
while ((j+jj < width) && (grid[j+jj][i] == '.'))
jj++;
if (j+jj < width)
printf("%d %d ", j+jj, i);
else
printf("%d %d ", -1, -1);
// Search for the next node on the row
int ii = 1;
while ((i+ii < height) && (grid[j][i+ii] == '.'))
ii++;
if (i+ii < height)
printf("%d %d\n", j, i+ii);
else
printf("%d %d\n", -1, -1);
}
}
}
}
width, height, grid = int(input()), int(input()), []
for i in range(height):
grid.append(list(input()))
for i in range(height):
for j in range(width):
if grid[i][j] == '0':
print("{} {} ".format(j, i), end="")
jj = 1;
while j+jj < width and grid[i][j+jj] == '.':
jj+=1
if j+jj < width:
print("{} {} ".format(j+jj, i), end="")
else:
print("-1 -1 ", end="")
ii = 1;
while i+ii < height and grid[i+ii][j] == '.':
ii+=1
if i+ii < height:
print("{} {} ".format(j, i+ii))
else:
print("-1 -1 ")