-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.java
86 lines (80 loc) · 2.66 KB
/
Maze.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package Racurcia;
/** This program finds out the possible paths to go out of a
* maze.
* The input is the mouse position (remember that columns and
* rows start at 0) and the maze pattern. The program uses
* backtracking strategy.
*/
public class Maze
{
private final static int MAX_ROW = 5;
private final static int MAX_COLUMN = 5;
private final static char OPEN = '-';
private final static char WAY_OUT = 's';
/**
* Returns true if position is on the outline,
* else – returns false.
*/
private static boolean exit (int row, int col)
{
return (row==0) || (row==MAX_ROW-1) ||
(col==0) || (col==MAX_COLUMN-1);
}
/**
* Prints the maze (matrix) as it is.
*/
public static void print (char [][] maze)
{
for (int i=0; i<MAX_ROW; i++)
{
for (int j=0; j<MAX_COLUMN; j++)
System.out.print(maze[i][j] + "\t");
System.out.println();
}
System.out.println();
}
/** This is a recursive method that look for a path out of the
* maze. The parameters are the maze and the current position
* of the mouse.
*/
public static void findWayOut (char [][] m, int row, int col)
{
// the temporary (local) array
char [][] maze = new char [MAX_ROW][MAX_COLUMN];
// copy the array given as a parameter to a local array.
for (int i=0; i<MAX_ROW; i++)
for (int j=0; j<MAX_COLUMN; j++)
maze [i][j] = m[i][j];
// mark the current position as a point on the path.
maze[row][col] = WAY_OUT;
// if the position of the mouse is on the outline of the
// maze – print the temporary matrix that contains the path
// out of the maze.
if (exit (row, col))
{
print (maze);
}
else
// the mouse is not at the exit of the maze seek for its
// way out with four directions:
{
if (maze[row-1][col] == OPEN)
findWayOut (maze, row-1, col);
if (maze[row][col+1] == OPEN)
findWayOut (maze, row, col+1);
if (maze[row+1][col] == OPEN)
findWayOut (maze, row+1, col);
if (maze[row][col-1] == OPEN)
findWayOut (maze, row, col-1);
}
}
public static void main(){
char maze [][] =
{ {'#' , '#' , '#' , '#' , '#'},
{'#' , '-' , '-' , '-' , '#'},
{'#' , '-' , '#' , '-' , '#'},
{'#' , '-' , '#' , '-' , '-'},
{'#' , '#' , '#' , '#' , '#'}, } ;
findWayOut(maze,1 ,1);
}
}