// Busca un camino en un laberinto: # es pared, E es la salida.
// Al terminar, + marca el camino encontrado y x las celdas de donde hubo que volver.
#include <stdio.h>

#define ROWS 7
#define COLS 11

void print_maze(char maze[][COLS + 1]) {
    for (int row = 0; row < ROWS; row++) {
        printf("%s\n", maze[row]);
    }
}

// Devuelve 1 si desde (row, col) se puede llegar a la salida.
int find_path(char maze[][COLS + 1], int row, int col) {
    if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
        return 0;
    }
    if (maze[row][col] == 'E') {
        return 1;
    }
    if (maze[row][col] != ' ') {
        return 0;
    }
    maze[row][col] = '+';
    if (find_path(maze, row - 1, col) || find_path(maze, row + 1, col)
        || find_path(maze, row, col - 1) || find_path(maze, row, col + 1)) {
        return 1;
    }
    maze[row][col] = 'x';
    return 0;
}

int main(void) {
    char open_maze[ROWS][COLS + 1] = {
        "###########",
        "#   #     #",
        "# # # ### #",
        "# #   #   #",
        "# ##### # #",
        "#       #E#",
        "###########",
    };
    char closed_maze[ROWS][COLS + 1] = {
        "###########",
        "#   #     #",
        "# # # ### #",
        "# #   #   #",
        "# #########",
        "#       #E#",
        "###########",
    };

    printf("hay camino: %d\n", find_path(open_maze, 1, 1));
    print_maze(open_maze);
    printf("hay camino: %d\n", find_path(closed_maze, 1, 1));
    print_maze(closed_maze);
    return 0;
}
