rambless

迷宫问题

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int x = in.nextInt();
            int y = in.nextInt();

            int[][] arr = new int[x][y];
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    arr[i][j] = in.nextInt();
                }
            }
            List<Point> list = new ArrayList<>();
            dfs(arr, 0, 0, list);
            for (int i = 0; i < list.size(); i++) {
                System.out.println("(" + list.get(i).x + "," + list.get(i).y + ")");
            }
        }
    }

    //先往右、再往下、再往左、最后往上:尽量别回头、找到一种结果就返回
    private static boolean dfs(int[][] arr, int x, int y, List<Point> list) {
        list.add(new Point(x, y));
        //标记已走过
        arr[x][y] = 1;
        //递归出口
        if (y == arr[0].length - 1 && x == arr.length - 1) {
            return true;
        }
        //能往右走
        if (y < arr[0].length - 1 && arr[x][y + 1] == 0) {
            if (dfs(arr, x, y + 1, list)) {
                return true;
            }
        }
        //能往下走
        if (x < arr.length - 1 && arr[x + 1][y] == 0) {
            if (dfs(arr, x + 1, y, list)) {
                return true;
            }
        }
        //能往左走
        if (y > 0 && arr[x][y - 1] == 0) {
            if (dfs(arr, x, y - 1, list)) {
                return true;
            }
        }
        //能往上走
        if (x > 0 && arr[x - 1][y] == 0) {
            if (dfs(arr, x - 1, y, list)) {
                return true;
            }
        }
        //没路了
        list.remove(list.size() - 1);
        //标记当前点为没走过
        arr[x][y] = 0;
        //回溯
        return false;
    }

    //每个点都寻找所有可能,最短路径一定先到达目标点
    //后到达(x,y)点的路B一定大于等于之前走过此点的路A,故路B在此点不用再做尝试,也就是每走一步标记已走
    private static List<Point> bfs(int[][] arr, int a, int b) {
        Deque<Point> queue = new LinkedList<>();
        queue.offer(new Point(0, 0, null));
        Point poll = null;
        int x;
        int y;
        while (!queue.isEmpty()) {
            poll = queue.poll();
            x = poll.x;
            y = poll.y;
            //如果是终点,则为要获取的元素
            if (x == a - 1 && y == b - 1) {
                //出现最优解,终止算法
                break;
            }
            //能往下走
            if (x < a - 1 && arr[x + 1][y] == 0) {
                queue.offer(new Point(x + 1, y, poll));
                arr[x + 1][y] = 1;
            }
            //能往右走
            if (y < b - 1 && arr[x][y + 1] == 0) {
                queue.offer(new Point(x, y + 1, poll));
                arr[x][y + 1] = 1;
            }
            //能往上走
            if (x > 0 && arr[x - 1][y] == 0) {
                queue.offer(new Point(x - 1, y, poll));
                arr[x - 1][y] = 1;
            }
            //能往左走
            if (y > 0 && arr[x][y - 1] == 0) {
                queue.offer(new Point(x, y - 1, poll));
                arr[x][y - 1] = 1;
            }
        }

        Deque<Point> stack = new LinkedList<>();
        while (poll != null) {
            stack.push(poll);
            poll = poll.p;
        }
        List<Point> list = new ArrayList<>();
        while (!stack.isEmpty()) {
            list.add(stack.poll());
        }
        return list;
    }

    static class Point {
        int x;
        int y;
        Point p;
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public Point(int x, int y, Point p) {
            this.x = x;
            this.y = y;
            this.p = p;
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务