001package algs41;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac Maze.java
005 *  Execution:    java Maze.java N
006 *  Dependecies:  StdDraw.java
007 *
008 *  Generates a perfect N-by-N maze using depth-first search with a stack.
009 *
010 *  % java Maze 62
011 *
012 *  Note: this program generalizes nicely to finding a random tree
013 *        in a graph.
014 *
015 *************************************************************************/
016
017public class XMaze {
018        private final int N;           // dimension of maze
019        private boolean[][] north;     // is there a wall to north of cell i, j
020        private boolean[][] east;
021        private boolean[][] south;
022        private boolean[][] west;
023        private boolean[][] visited;
024        //private double size;
025        private boolean done = false;
026
027        public XMaze(int N) {
028                this.N = N;
029                StdDraw.setXscale(0, N+2);
030                StdDraw.setYscale(0, N+2);
031                init();
032                generate();
033        }
034
035        private void init() {
036                // initialize border cells as already visited
037                visited = new boolean[N+2][N+2];
038                for (int x = 0; x < N+2; x++) visited[x][0] = visited[x][N+1] = true;
039                for (int y = 0; y < N+2; y++) visited[0][y] = visited[N+1][y] = true;
040
041
042                // initialize all walls as present
043                north = new boolean[N+2][N+2];
044                east  = new boolean[N+2][N+2];
045                south = new boolean[N+2][N+2];
046                west  = new boolean[N+2][N+2];
047                for (int x = 0; x < N+2; x++)
048                        for (int y = 0; y < N+2; y++)
049                                north[x][y] = east[x][y] = south[x][y] = west[x][y] = true;
050        }
051
052
053        // generate the maze
054        private void generate(int x, int y) {
055                visited[x][y] = true;
056
057                // while there is an unvisited neighbor
058                while (!visited[x][y+1] || !visited[x+1][y] || !visited[x][y-1] || !visited[x-1][y]) {
059
060                        // pick random neighbor (could use Knuth's trick instead)
061                        while (true) {
062                                double r = Math.random();
063                                if (r < 0.25 && !visited[x][y+1]) {
064                                        north[x][y] = south[x][y+1] = false;
065                                        generate(x, y + 1);
066                                        break;
067                                }
068                                else if (r >= 0.25 && r < 0.50 && !visited[x+1][y]) {
069                                        east[x][y] = west[x+1][y] = false;
070                                        generate(x+1, y);
071                                        break;
072                                }
073                                else if (r >= 0.5 && r < 0.75 && !visited[x][y-1]) {
074                                        south[x][y] = north[x][y-1] = false;
075                                        generate(x, y-1);
076                                        break;
077                                }
078                                else if (r >= 0.75 && r < 1.00 && !visited[x-1][y]) {
079                                        west[x][y] = east[x-1][y] = false;
080                                        generate(x-1, y);
081                                        break;
082                                }
083                        }
084                }
085        }
086
087        // generate the maze starting from lower left
088        private void generate() {
089                generate(1, 1);
090                // delete some random walls
091                for (int i = 0; i < N; i++) {
092                        int x = (int) (1 + Math.random() * (N-1));
093                        int y = (int) (1 + Math.random() * (N-1));
094                        north[x][y] = south[x][y+1] = false;
095                }
096                // add some random walls
097                for (int i = 0; i < N; i++) {
098                        int x = (int) (N / 2 + Math.random() * (N / 2));
099                        int y = (int) (N / 2 + Math.random() * (N / 2));
100                        east[x][y] = west[x+1][y] = true;
101                }
102        }
103
104
105
106        // solve the maze using depth first search
107        private void solve(int x, int y) {
108                if (x == 0 || y == 0 || x == N+1 || y == N+1) return;
109                if (done || visited[x][y]) return;
110                visited[x][y] = true;
111
112                StdDraw.setPenColor(StdDraw.BLUE);
113                StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
114                StdDraw.show(30);
115
116                // reached middle
117                if (x == N/2 && y == N/2) done = true;
118
119                if (!north[x][y]) solve(x, y + 1);
120                if (!east[x][y])  solve(x + 1, y);
121                if (!south[x][y]) solve(x, y - 1);
122                if (!west[x][y])  solve(x - 1, y);
123
124                if (done) return;
125
126                StdDraw.setPenColor(StdDraw.GRAY);
127                StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
128                StdDraw.show(30);
129        }
130
131        // solve the maze starting from the start state
132        public void solve() {
133                for (int x = 1; x <= N; x++)
134                        for (int y = 1; y <= N; y++)
135                                visited[x][y] = false;
136                done = false;
137                solve(1, 1);
138        }
139
140        // display the maze in turtle graphics
141        public void draw() {
142                StdDraw.setPenColor(StdDraw.RED);
143                StdDraw.filledCircle(0.5*N + 0.5, 0.5*N + 0.5, 0.375);
144                StdDraw.filledCircle(1.5, 1.5, 0.375);
145
146                StdDraw.setPenColor(StdDraw.BLACK);
147                for (int x = 1; x <= N; x++) {
148                        for (int y = 1; y <= N; y++) {
149                                if (south[x][y]) StdDraw.line(x, y, x + 1, y);
150                                if (north[x][y]) StdDraw.line(x, y + 1, x + 1, y + 1);
151                                if (west[x][y])  StdDraw.line(x, y, x, y + 1);
152                                if (east[x][y])  StdDraw.line(x + 1, y, x + 1, y + 1);
153                        }
154                }
155                StdDraw.show(1000);
156        }
157
158
159
160        // a test client
161        public static void main(String[] args) {
162                args = new String[] { "20" };
163
164                int N = Integer.parseInt(args[0]);
165                XMaze maze = new XMaze(N);
166                StdDraw.show(0);
167                maze.draw();
168                maze.solve();
169        }
170
171}
172