【华为笔试】【DFS】【Java】迷宫问题

本文介绍如何使用递归算法解决一个二维数组表示的迷宫问题,从左上角到右下角寻找最短路径,输入为矩阵形式,输出路径坐标序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义一个二维数组 N*M ,如 5 × 5 数组下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的路线。入口点为[0,0],既第一格是可以走的路。
数据范围: 2≤n,m≤10 , 输入的内容只包含 0≤val≤1。
输入描述:
输入两个整数,分别表示二维数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
输出描述:
左上角到右下角的最短路径,格式如样例所示。
示例1
输入:
5 5
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
输出:
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)
示例2
输入:
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 1
0 1 1 1 0
0 0 0 0 0
输出:
(0,0)
(1,0)
(2,0)
(3,0)
(4,0)
(4,1)
(4,2)
(4,3)
(4,4)
说明:注意:不能斜着走!!!

public class MazeProblem2 {

    private static int n1;
    private static int m1;
    private static int[][] arr;//邻接表
    private static boolean[][] mark;
    private static int endX1;
    private static int endY1;
    private static List<int[]> path = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] sa = sc.nextLine().split(" ");
        n1 = Integer.parseInt(sa[0]);
        m1 = Integer.parseInt(sa[1]);
        endX1 = n1 -1;
        endY1 = m1 -1;
        arr = new int[n1][m1];
        mark = new boolean[n1][m1];
        for (int i = 0; i < n1; i++) {
            String[] split = sc.nextLine().split(" ");
            for (int j = 0; j < m1; j++) {
                arr[i][j] = Integer.parseInt(split[j]);
            }
        }
        dfs(0, 0);
        Collections.reverse(path);
        for (int i = 0; i < path.size(); i++) {
            int[] res = path.get(i);
            System.out.println("(" + res[0] + "," +res[1]+ ")");
        }
    }
    //dfs就是递归求最小值
    private static boolean dfs(int x1,int y1){
        if (x1 == endX1 && y1 == endY1){
            path.add(new int[]{x1,y1});
            return true;
        }
        int[][] next = {{1,0},{0,1},{0,-1},{-1,0}};
        boolean flag = false;
        for (int i = 0; i < 4; i++) {
            int nextX = x1 + next[i][0];
            int nextY = y1 + next[i][1];
            if (nextX < 0 || nextX >= n1 || nextY<0 || nextY>= m1){
                continue;
            }
            if (arr[nextX][nextY] == 0 && !mark[nextX][nextY]){//下个点是0 且未做标记
                mark[nextX][nextY] = true;
                if (dfs(nextX, nextY)) {
                    flag = true;
                    path.add(new int[]{x1,y1});
                }
                mark[nextX][nextY] = false;
            }
        }
        return flag;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值