【牛客网】迷宫问题

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

采用搜索回溯的方法。
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int row = in.nextInt();
            int column = in.nextInt();
            int[][] arr = new int[row][column];
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    arr[i][j] = in.nextInt();
                }
            }
            boolean[][] mark = new boolean[row][column];
            LinkedList<int[]> path = new LinkedList<>();
            shortPath = new LinkedList<>();
            shortPath.add(new int[]{6, 6});
            path.add(new int[]{0, 0});
            leastStep = Integer.MAX_VALUE;
            mark[0][0] = true;
            dfs(arr, mark, 0, 0, path, row, column);
            for (int[] ints : shortPath) {
                System.out.println("(" + ints[0] + "," + ints[1] + ")");
            }
        }
    }

    public static int leastStep;
    public static LinkedList<int[]> shortPath;

    /**
     * @param arr    迷宫
     * @param mark   标记是否走过
     * @param i      横坐标
     * @param j      纵坐标
     * @param path   路径
     * @param row    迷宫的行数
     * @param column 迷宫的列数
     */
    private static void dfs(int[][] arr,
                            boolean[][] mark,
                            int i,
                            int j,
                            LinkedList<int[]> path,
                            int row,
                            int column) {

        if (i == row - 1 && j == column - 1) {
            // 到出口了。
            if (path.size() < leastStep) {
                shortPath = new LinkedList<>(path);// 如果这个路径比较短的话,保存。
                leastStep = path.size();
            }
            return;
        }

        // 进行遍历。四个方向。
        int[][] move = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

        for (int k = 0; k < 4; k++) {

            int x = i + move[k][0];
            int y = j + move[k][1];

            if (x < 0 || x >= row || y < 0 || y >= column) {
                continue;
            }

            if (mark[x][y]) { // 说明走过了
                continue;
            }

            // 说明在合理的范围中,并且这个位置没有走过。
            if (arr[x][y] == 1) { // 说明是墙
                continue;
            }

            // 这个位置不是墙。还在合理的范围,还没走过。
            mark[x][y] = true;

            path.add(new int[]{x, y}); // path中添加这个坐标

            dfs(arr, mark, x, y, path, row, column);

            path.removeLast();
            mark[x][y] = false;
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值