题解 | #迷宫问题#

迷宫问题

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

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static int xNum = 0;
    public static int yNum = 0;
    public static int[][] maze = null;
    public static boolean findRoute = false;
    public static List<String> findRouteList = null;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = -3;

        List<Node1> line = new LinkedList<>();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            n++;
            int a = in.nextInt();
            if (n == -2) {
                yNum = a;
                continue;
            } else if (n == -1) {
                xNum = a;
                maze = new int[yNum][xNum];
                n = -1;
                continue;
            }

            int i = n % xNum;
            int j = n / xNum;
            init(i, j, a);
        }
        int i = 0, j = 0;
        Node1 node1 = new Node1();
        node1.num = 0;
        node1.i = i;
        node1.j = j;
        setNode1(node1, i, j);

        findRouteList.forEach(item -> {
            System.out.println(item);
        });

    }

    private static void init(int i, int j, int a) {
        maze[j][i] = a;
    }

    private static boolean cross(int i, int j) {

        if(i<0 || j<0 || i >= xNum || j >= yNum){
            return false;
        }
        int a = maze[j][i];
        if (a == 0) {
            return true;
        }
        return false;
    }

    private static String getRoute(int i, int j) {
        return "(" + j + "," + i + ")";
    }

    private static boolean setFindEndRoute(Node1 node1){
        if(node1.i == xNum-1 && node1.j == yNum -1){
            findRouteList = node1.route;
            return true;
        }
        return false;
    }


    private static void setNode1(Node1 node1, int i, int j) {

        if(node1.route.contains(getRoute(i,j))){
            return;
        }

        node1.route.add(getRoute(i,j));
        if(setFindEndRoute(node1)){
            return;
        }
        hasNext(node1, i, j, -1,  0);
        hasNext(node1, i, j, 1,  0);
        hasNext(node1, i, j, 0,  -1);
        hasNext(node1, i, j, 0,  1);
    }

    private static void hasNext(Node1 node1, int i, int j, int moveX,
                                   int moveY) {

        int i1 = i + moveX;
        int j1 = j + moveY;;
        if (!cross(i1, j1)) {
            return;
        }

        node1.hasNext = true;

        Node1 next = new Node1();
        next.num = node1.num +1;
        next.i = i1;
        next.j = j1;
        node1.route.forEach(item -> {
            next.route.add(item);
        });
        //next.route.add(getRoute(i1,j1));

        if (moveX == -1 && moveY == 0) {
            node1.nextLetft = next;
        } else if (moveX == 1 && moveY == 0) {
            node1.nextRight = next;
        } else if (moveX == 0 && moveY == 1) {
            node1.nextDown = next;
        } else if (moveX == 0 && moveY == -1) {
            node1.nextUp = next;
        }
        
        setNode1(next, i1, j1);
    }

    static class Node1 {

        public int num;
        public int i;
        public int j;
        public Node1 nextLetft = null;
        public Node1 nextRight = null;
        public Node1 nextUp = null;
        public Node1 nextDown = null;
        public boolean hasNext = false;
        public List<String> route = new LinkedList<>();
    }
}

解题思路和91有点像,解了一天也是有了个好的成果。

#努力刷题下#
雪域灰灰刷题笔记 文章被收录于专栏

雪域灰灰刷题笔记

全部评论

相关推荐

03-15 00:45
已编辑
中国科学院大学 Java
问的很简单都秒了,但是面试官没开摄像头,疑似kpi,无后续。--------------------3/14更新,3/12通知给了口头offer,3/13发了意向书,已拒。一面(35min)(25/3/6)(无后续)&nbsp;&nbsp;&nbsp;&nbsp;1、自我介绍&nbsp;&nbsp;&nbsp;&nbsp;2、介绍一下你的那个Python相关项目(本科毕设,web系统+算法模型提供部分接口)&nbsp;&nbsp;&nbsp;&nbsp;3、Java面向对象有哪些特点呢?详细说一下。&nbsp;&nbsp;&nbsp;&nbsp;4、介绍一下hashmap;为什么要把链表转换为红黑树呢?红黑树查找的时间复杂度?1.7和1.8的区别。&nbsp;&nbsp;&nbsp;&nbsp;5、介绍一下concurrentHashmap。&nbsp;&nbsp;&nbsp;&nbsp;6、synchronized锁和Lock锁有什么区别?&nbsp;&nbsp;&nbsp;&nbsp;7、公平锁的一个底层是怎么实现的呢?&nbsp;&nbsp;&nbsp;&nbsp;8、线程池的核心参数、拒绝策略、提交一个任务执行流程?&nbsp;&nbsp;&nbsp;&nbsp;9、spring有哪些特点?(ioc/aop)&nbsp;&nbsp;&nbsp;&nbsp;10、spring中对于循环依赖是怎么解决的?&nbsp;&nbsp;&nbsp;&nbsp;11、MySQL和redis的区别?&nbsp;&nbsp;&nbsp;&nbsp;12、MySQL的索引结构是什么?&nbsp;&nbsp;&nbsp;&nbsp;13、MySQL的事务有哪些特性?怎么保证?&nbsp;&nbsp;&nbsp;&nbsp;14、MySQL的默认隔离级别?可重复读是怎么做到的呢?&nbsp;&nbsp;&nbsp;&nbsp;15、介绍一下MVCC和快照读readview。&nbsp;&nbsp;&nbsp;&nbsp;16、一般在什么场景下会使用redis?&nbsp;&nbsp;&nbsp;&nbsp;17、对于大量的请求,如果此时缓存中还没有写入数据怎么办?&nbsp;&nbsp;&nbsp;&nbsp;18、介绍一下redis实现的分布式锁。&nbsp;&nbsp;&nbsp;&nbsp;19、有用过es和mongo&nbsp;DB吗?(知道,没用过)&nbsp;&nbsp;&nbsp;&nbsp;20、消息中间件用过吗?说一下你的使用场景?&nbsp;&nbsp;&nbsp;&nbsp;21、一个场景,如果说有一个接口响应的比较慢,如果说让你排查,你会怎么去排查?(上下游接口、大key问题,只答了两,后面试官补充)&nbsp;&nbsp;&nbsp;&nbsp;无手撕,反问业务。
胖墩墩的查理在学c语言:哥们我是五号面的 流程差不多
查看21道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务