#include <stdio.h>

#define SIZE 12

void printMaze(char maze[][SIZE])
{
    for (int row = 0; row < SIZE; ++row)
    {
        for (int column = 0; column < SIZE; ++column)
        {
            printf("%c ", maze[row][column]);
        }

        puts("");
    }

    puts("");
}

int mazeTraverse(char maze[][SIZE],
                 int row,
                 int column)
{
    /*
       If the current position is outside the maze,
       then the exit has been reached.
    */

    if (row < 0 || row >= SIZE ||
        column < 0 || column >= SIZE)
    {
        return 1;
    }

    if (maze[row][column] != '.')
    {
        return 0;
    }

    maze[row][column] = 'X';

    printMaze(maze);

    /*
       Try moving in four directions.
       The order is:
       right, down, left, up.
    */

    if (mazeTraverse(maze, row, column + 1))
    {
        return 1;
    }

    if (mazeTraverse(maze, row + 1, column))
    {
        return 1;
    }

    if (mazeTraverse(maze, row, column - 1))
    {
        return 1;
    }

    if (mazeTraverse(maze, row - 1, column))
    {
        return 1;
    }

    /*
       This position was not part of the correct path,
       so change it back.
    */

    maze[row][column] = '.';

    return 0;
}

int main(void)
{
    char maze[SIZE][SIZE] =
    {
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
        { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
        { '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
        { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '#', '#', '.', '#', '.', '.', '.' },
        { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }
    };

    printf("Maze before traversal:\n\n");
    printMaze(maze);

    if (mazeTraverse(maze, 1, 1))
    {
        printf("The maze was solved.\n");
    }
    else
    {
        printf("No path was found.\n");
    }

    printf("\nMaze after traversal:\n\n");
    printMaze(maze);

    return 0;
}