APU, Init Phase


This puzzle is a prelude to a hard puzzle. In my opinion, it does not really deserve to be in the medium section. It involves a graph traversal, but in a 2-dimension graph, so there is no need to use a proprer graph structure. The aim is, to find for each node the next node to the right, and the next node below.

C

#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);
                        
            }
        }
    }
}
            

Python

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 ")