The idea is to use a recursive loop judge found the path to the exit of
public class MiGong { public static void main(String[] args) { //地图 int[][] map = new int[8][7]; //墙 for (int i = 0; i < 7; i++) { map[0][i] = 1; map[7][i] = 1; } for (int i = 1; i < 8; i++) { map[i][0] = 1; map[i][6] = 1; } map[3][1] = 1; map[3][2] = 1; map[1][2] = 1; map[2][2] = 1; //输出地图 for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { System.out.print(map[i][j]+" "); } System.out.println(); } System.out.println(); // recursive backtracking to find a way setWay (Map,. 1,. 1 ); for ( int I = 0; I <. 8; I ++ ) { for ( int J = 0; J <. 7; J ++ ) { System.out.print(map[i][j]+" "); } System.out.println(); } } // 0 through 1 represents no wall 2 shows can go through 3 represent a dead end / ** * @param the Map Map * @Param I starting position * @param j * @Return found otherwise returns true to false * / public static Boolean setWay ( int [] [] Map, int I, int J) { IF (Map [. 6] [. 5] == 2 ) { return true ; } else { if (map[i][j] == 0) { map[i][j] = 2; if (setWay(map, i + 1, j)) { return true; } else if (setWay(map, i, j + 1)) { return true; } else if (setWay(map, i - 1, j)) { return true; } else if (setWay(map, i, j - 1)) { return true; } else { map[i][j] = 3; return false; } } else { return false; } } } }