HJ43 迷宫问题

牛客华为机试题库【题号 HJ开头】(重点看)
牛客在线编程算法篇【题号NC开头】
剑指offer【题号 JZ开头】
力扣

1)原题链接

2)已有题解

3)代码

package bfs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

import static java.lang.System.in;

/**
 * HJ43 迷宫问题
 */
public class HJ43MazeProblem {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        int[] size = Arrays.stream(bf.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        // 构造网格
        int row = size[0]; // 行
        int column = size[1]; // 列
        int[][] grid = new int[row][column];
        for (int i = 0; i < row; i++) {
            int[] col = Arrays.stream(bf.readLine().split(" ")).limit(column).mapToInt(Integer::parseInt).toArray();
            grid[i] = col;
        }
        bf.close();

        Point minDistance = getMinDistance(grid);

        Deque<int[]> res = new LinkedList<>();
        while (minDistance != null) {
            res.addFirst(getCoordinate(minDistance.row, minDistance.column));
            minDistance = minDistance.father;
        }

        res.stream().forEach(p -> System.out.println("(" + p[0] + "," + p[1] + ")"));
    }


    public static Point getMinDistance(int[][] grid) {
        Deque<Point> queue = new LinkedList<>(); // 队列记录下一步的所有情况
        boolean[][] visited = new boolean[grid.length][grid[0].length];//表示走过的位置,TRUE→走过,FALSE→没走过

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (i == 0 && j == 0) { // 表示找到入口,这里其实不用两层循环、只是练习一下这个思路
                    queue.add(getNode(i, j, null));
                    visited[i][j] = true;// 其实位置标记为已走过
                    break;
                }
            }
        }

        int[][] nestStep = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上、下、左、右

        while (!queue.isEmpty()) {
            int queueSize = queue.size();
            for (int m = 0; m < queueSize; m++) {
                Point poll = queue.poll();
                for (int[] ints : nestStep) { // 上、下、左、右、四种可能性都遍历走一遍
                    int newX = poll.row + ints[0]; // 获取下一步的X坐标
                    int newY = poll.column + ints[1]; // 获取下一步的Y坐标
                    if (isValidInGrid(newX, newY, grid) && newX == grid.length - 1 && newY == grid[0].length - 1) { //范围内,如果下一步时出口的话,直接走一步出去
                        return getNode(newX, newY, poll); // 最后一个节点
                    }
                    if (isValidInGrid(newX, newY, grid) && !visited[newX][newY] && grid[newX][newY] == 0) {//范围内,没走过,不是障碍物
                        queue.add(getNode(newX, newY, poll));
                        visited[newX][newY] = true;//标记已经处理过
                    }
                }
            }
        }

        return null; // 表示没找到
    }

    public static int[] getCoordinate(int i, int j) {
        return new int[]{i, j};
    }

    public static Point getNode(int i, int j, Point fatherNode) {
        return new Point(i, j, fatherNode);
    }

    public static boolean isValidInGrid(int i, int j, int[][] arr) {
        return 0 <= i && i < arr.length && 0 <= j && j < arr[i].length;
    }

}

class Point {
    int row;
    int column;
    Point father;

    Point(int px, int py, Point father) {
        this.row = px;
        this.column = py;
        this.father = father;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值