现有一个
n∗m
大小的迷宫,其中1
表示不可通过的墙壁,0
表示平地。每次移动只能向上下左右移动一格,且只能移动到平地上。假设左上角坐标是(1,1)
,行数增加的方向为x
增长的方向,列数增加的方向为y
增长的方向,求从迷宫左上角到右下角的最少步数的路径。
第一行两个整数
n、m(2≤n≤100,2≤m≤100)
,分别表示迷宫的行数和列数;接下来
n
行,每行m
个整数(值为0
或1
),表示迷宫。
从左上角的坐标开始,输出若干行(每行两个整数,表示一个坐标),直到右下角的坐标。
数据保证最少步数的路径存在且唯一。
3 3
0 1 0
0 0 0
0 1 0
1 1
2 1
2 2
2 3
3 3
假设左上角坐标是(1,1)
,行数增加的方向为x
增长的方向,列数增加的方向为y
增长的方向。
可以得到从左上角到右下角的最少步数的路径为:(1,1)=>(2,1)=>(2,2)=>(2,3)=>(3,3)
Pair prePosition[n][m]
,其中Pair
是我们的自定义类,其存放点的坐标信息,而在prePosition
数组中则是存放到达当前点的上一个点的坐标信息,如prePosition[i][j]=new Pair(x,y)
代表广度优先搜索中(x,y)的下一个点是(i,j)
,初始(0,0)
的上一个坐标是(-1,-1)
。package homework;import java.util.LinkedList;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();int arr[][] = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {arr[i][j] = scanner.nextInt();}}BFS(arr, n, m);}public static void BFS(int arr[][], int n, int m) {LinkedList queue = new LinkedList();// 广度遍历时,(x,y)下标对应的上一个坐标Pair prePosition[][] = new Pair[n][m];Pair pair = new Pair(0, 0);prePosition[0][0] = new Pair(-1, -1);queue.addLast(pair);while (!queue.isEmpty()) {int size = queue.size();for (int i = 0; i < size; i++) {Pair poll = queue.poll();arr[poll.row][poll.cols] = 1;if (poll.row == n - 1 && poll.cols == m - 1) {printCoord(prePosition, n - 1, m - 1);return;} else {Pair pre = new Pair(poll.row, poll.cols);if (poll.row - 1 >= 0 && arr[poll.row - 1][poll.cols] == 0) {queue.addLast(new Pair(poll.row - 1, poll.cols));prePosition[poll.row - 1][poll.cols] = pre;}if (poll.row + 1 < n && arr[poll.row + 1][poll.cols] == 0) {queue.addLast(new Pair(poll.row + 1, poll.cols));prePosition[poll.row + 1][poll.cols] = pre;}if (poll.cols + 1 < m && arr[poll.row][poll.cols + 1] == 0) {queue.addLast(new Pair(poll.row, poll.cols + 1));prePosition[poll.row][poll.cols + 1] = pre;}if (poll.cols - 1 >= 0 && arr[poll.row][poll.cols - 1] == 0) {queue.addLast(new Pair(poll.row, poll.cols - 1));prePosition[poll.row][poll.cols - 1] = pre;}}}}}static void printCoord(Pair prePosition[][], int row, int cols) {if (row != -1 && cols != -1) {Pair pair = prePosition[row][cols];printCoord(prePosition, pair.row, pair.cols);System.out.println((row + 1) + " " + (cols + 1));}}}class Pair {int row;int cols;public Pair(int x, int y) {this.row = x;this.cols = y;}}
上一篇:QML- 对象属性