深度优先访问+回溯实现迷宫问题
1为墙,0为可走的路,代码有注释。
import java.util.Scanner; public class maze { //地图的大小 //用来保存地图 static int n = 6, m = 5, start_x, start_y, end_x, end_y; //n22 m21 // static int[][] map = { // {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // {1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1}, // {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1}, // {1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, // {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1}, // {1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, // {1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1}, // {1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1}, // {1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1}, // {1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1}, // {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1}, // {1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1}, // {1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1}, // {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1}, // {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}; //// n=6,m=5 static int[][] map = { {1,1,1,1,1}, {1,0,0,0,1}, {1,0,1,0,1}, {1,0,1,0,1}, {1,0,0,0,1}, {1,1,1,1,1}}; //用来保存是否被访问过 static int[][] isVisited = new int[n][m]; //记录所有可行路径下标 static int[][] train = new int[2500][2]; //遵循 左下右上 的顺序 static int[][] path = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; //记录所有到达的路径数量 static int count; //记录走的步数,方便回溯打印 static int k; //判断是否已经到达终点 static int flag; static boolean f = false; public static void main(String[] args) { Scanner sc = new Scanner(System.in); //输出地图 System.out.println("地图的情况"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.print(map[i][j] + " "); } System.out.println(); } // System.out.println("(1,1)(1,19)可作为起点---(20,19)(20,3)可作为终点---也可另寻别路"); //输入判断操作 while (!f) { System.out.print("请输入起点坐标:"); start_x = sc.nextInt(); start_y = sc.nextInt(); System.out.print("请输入终点坐标:"); end_x = sc.nextInt(); end_y = sc.nextInt(); //判断要查询的起点和终点坐标 是否符合标准 if (!isStandard(start_x, start_y)) { System.out.println("输入的起点坐标有误~~~"); } else if (!isStandard(end_x, end_y)) { System.out.println("输入的终点坐标有误~~~"); } else if (start_x == end_x && start_y == end_y) { System.out.println("起点终点不能相同~~~"); } else { dfs(start_x, start_y); if (count == 0) { System.out.println("没路"); } return; } } } //传进来起点坐标 public static void dfs(int x, int y) { if (x == end_x && y == end_y) { count++; printPath(); return; } for (int i = 0; i < path.length; i++) { //开始四个方向分别遍历 int next_x = x + path[i][0]; int next_y = y + path[i][1]; isVisited[x][y] = 1; //判断是否符合标准 if (isStandard(next_x, next_y)) { isVisited[next_x][next_y] = 1; train[k][0] = x; train[k][1] = y; k++; //进行递归寻找下一个可行的路 dfs(next_x,next_y); //若到了终点置零 isVisited[next_x][next_y] = 0; k--; } } } //打印走过的路径 public static void printPath() { for(int i = 0;i<=k-1;i++){ System.out.print("[" + train[i][0] + "," + train[i][1] + "]"+"->"); } System.out.print("[" + end_x + "," + end_y + "]"); System.out.println(); } //判断这个点是否符合标准 static boolean isStandard(int x, int y) { if (x < n && x > 0 && y > 0 && y < m && map[x][y] == 0 && isVisited[x][y] == 0) { return true; } return false; } }