The mouse maze is the basic question of recursive solution, we use 2 in the two-dimensional array to represent the maze wall, use one to denote the mouse's walking path, try to find the path from the entrance to the exit by the program.
public class Mousealt {
Static int[][] Maze = {
{2,2,2,2,2,2,2},
{2,0,0,0,0,0,2},
{2,0,2,0,2,0,2},
{2,0,0,0,0,2,2},
{2,2,0,2,0,2,2},
{2,0,0,0,0,0,2},
{2,2,2,2,2,2,2}
};
/**
* @param args
*/
static int starti = 1;
static int startj = 1;
static int endi = 5;
static int endj = 5;
static int success = 0;
public static void Main (string[] args) {
System.out.println ("Show Maze: \ n");
for (int i = 0; i < 7; i++) {
for (int j = 0; J < 7; J + +) {
if (maze[i][j] = = 2) {
System.out.print ("█");
}else {
System.out.print ("*");
}
}
System.out.println ();
}
if (Visit (Starti, STARTJ) ==0) {
System.out.println ("No Exit Found");
} else {
SYSTEM.OUT.PRINTLN ("Display path");
for (int i = 0; i < 7; i++) {
for (int j = 0; J < 7; J + +) {
if (maze[i][j] = = 2) {
System.out.print ("█");
} else if (maze[i][j] = = 1) {
System.out.print ("◇");
}else {
System.out.print ("*");
}
}
System.out.println ();
}
}
}
public static int visit (int i, int j) {
MAZE[I][J] = 1;
if (i = = Endi && j = = Endj) {
Success = 1;
}
if (Success! = 1 && maze[i][j+1] = = 0) Visit (i, j+1);
if (Success! = 1 && maze[i+1][j] = = 0) visit (i+1,j);
if (Success! = 1 && maze[i][j-1] = = 0) Visit (i, j-1);
if (Success! = 1 && maze[i-1][j] = = 0) visit (i-1,j);
if (Success! = 1)
MAZE[I][J] = 0;
return success;
}
}
The mouse goes astray The official Java implementation