题解 | #迷宫问题#

迷宫问题

https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc

深度优先,其实不用那么代码,把代码的动作归纳一下就行

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static List<List<ZuoBiao>> shortPath = new ArrayList<>();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();

            int[][] mg = new int[a][b];

            for (int i = 0; i < a; i++) {
                for (int j = 0; j < b; j++) {
                    mg[i][j] = in.nextInt();
                }
            }


            move(mg, new ZuoBiao(0, 0), new ArrayList<>(), new HashSet<>());
            Collections.sort(shortPath, new Comparator<List<ZuoBiao>>() {
                @Override
                public int compare(List<ZuoBiao> o1, List<ZuoBiao> o2) {
                    return o1.size() - o2.size();
                }
            });

            for (ZuoBiao zuoBiao : shortPath.get(0)) {
                System.out.println(zuoBiao);
            }
        }
    }


    private static void move(int[][] mg, ZuoBiao curZB, ArrayList<ZuoBiao> steps,
                             Set<String> zbJL) {
        //结束条件 x ,y 等 mg 的两个length-1
        steps.add(curZB);
        zbJL.add(curZB.toString());
        if (curZB.x == mg.length - 1 && curZB.y == mg[0].length - 1) {
            //如果走完,则记录一条记录
            List<ZuoBiao> zuoBiaos = new ArrayList<>();
            zuoBiaos.addAll(steps);
            shortPath.add(zuoBiaos);
            return;
        }


        // 向左
        //没有碰壁
        if (curZB.y - 1 >= 0 && mg[curZB.x][curZB.y - 1] != 1) {
            ZuoBiao zuoBiao = new ZuoBiao(curZB.x, curZB.y - 1);
            // 不走回头路
            if (!zbJL.contains(zuoBiao.toString())) {
                move(mg, zuoBiao, steps, zbJL);
                steps.remove(steps.size() - 1);
                zbJL.remove(zuoBiao.toString());
            }
        }
        // 向右
        if (curZB.y + 1 < mg[0].length  && mg[curZB.x][curZB.y + 1] != 1) {
            ZuoBiao zuoBiao = new ZuoBiao(curZB.x, curZB.y + 1);
            if (!zbJL.contains(zuoBiao.toString())) { //防止走回头路
                move(mg, zuoBiao, steps, zbJL);

                //退回原来的步
                steps.remove(steps.size() - 1);
                zbJL.remove(zuoBiao.toString());
            }

        }
        // 向下
        if (curZB.x + 1 < mg.length  && mg[curZB.x + 1][curZB.y] != 1) {
            ZuoBiao zuoBiao = new ZuoBiao(curZB.x + 1, curZB.y);
            if (!zbJL.contains(zuoBiao.toString())) { //防止走回头路
                move(mg, zuoBiao, steps, zbJL);
                steps.remove(steps.size() - 1);
                zbJL.remove(zuoBiao.toString());
            }
        }
        // 向上
        if (curZB.x - 1 >= 0 && mg[curZB.x - 1][curZB.y] != 1) {
            ZuoBiao zuoBiao = new ZuoBiao(curZB.x - 1, curZB.y);
            if (!zbJL.contains(zuoBiao.toString())) { //防止走回头路
                move(mg, zuoBiao, steps, zbJL);
                steps.remove(steps.size() - 1);
                zbJL.remove(zuoBiao.toString());
            }
        }


    }


    public static class ZuoBiao {
        int x;
        int y;

        public ZuoBiao(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
11-15 18:39
已编辑
西安交通大学 Java
全村最靓的仔仔:卧槽,佬啥bg呢,本也是西交么
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务