about summary refs log tree commit diff stats
path: root/algorithms/java/DFS.java
blob: 8d1e3411573d683560d759e40f974105f1241715 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .
public class DFS {
    static int[][] directions = new int[][]{
        {+0, +1}, // Right.
        {+1, +0}, // Down.
        {+0, -1}, // Left.
        {-1, +0}, // Up.
    };

    static void traverse(int x, int y, char[][] maze, boolean[][] visited) {
        for (int i = 0; i < 4; i++) {
            int curx = x + directions[i][0];
            int cury = y + directions[i][1];

            // Out of bounds check.
            if (curx < 0 || cury < 0
                || curx > maze.length - 1 || cury > maze.length - 1)
                continue;

            // Marker cells.
            if (maze[curx][cury] == '$')
                System.out.print("|");
            else if (maze[curx][cury] == '#')
                System.out.print("!");

            // Print the maze on every iteration.
            for(int j = 0; j < maze.length; j++)
                for(int k = 0; k < maze[j].length; k++)
                    if (j == curx && k == cury)
                        System.out.print("@");
                    else
                        System.out.print(visited[j][k] ? "-" : maze[j][k]);
            System.out.println();

            // Found a solution, exiting.
            if (maze[curx][cury] == '$')
                System.exit(0);

            if (visited[curx][cury]) {
                continue;
            } else if (maze[curx][cury] == '.') {
                visited[curx][cury] = true;
                traverse(curx, cury, maze, visited);
                visited[curx][cury] = false;
            }
        }
    }

    public static void main(String[] args) {
        char[][] maze = {
            {'.', '#', '.'},
            {'.', '.', '.'},
            {'.', '.', '$'}
        };

        boolean[][] visited = new boolean[maze.length][maze[0].length];

        for (int i = 0; i < maze.length; i++)
            for (int j = 0; j < maze[i].length; j++)
                visited[i][j] = false;

        System.out.println(String.format("rows:%d cols:%d", maze.length, maze[0].length));

        // Print the maze.
        for(int j = 0; j < maze.length; j++)
            for(int k = 0; k < maze[j].length; k++)
                    System.out.print(maze[j][k]);
        System.out.println();

        // Start at 0,0.
        visited[0][0] = true;
        traverse(0, 0, maze, visited);
    }
}