蓝桥 迷宫 BFS最短路 阅读理解 java

🍑 算法题解专栏


🍑 迷宫

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

输入

2 1
1 1 2 2 

输出

0.75

⭐ 每个点可能有多个传送门

⭐ 数组一定要开到足够大

🍑 ArrayList存传送门版

import java.util.*;

public class Main
{
    static int N = 5, n, m;
    static int[][] g = new int[N][N];
    static boolean[][] st = new boolean[N][N];
    static ArrayList[][] to = new ArrayList[N][N];
    static int[][] dist = new int[N][N];
    static int[] dx = { -1, 0, 1, 0 };
    static int[] dy = { 0, 1, 0, -1 };

    static class Node
    {
        int x;
        int y;

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

    static void bfs(int sx, int sy)
    {
        st[sx][sy] = true;
        dist[sx][sy] = 0;
        LinkedList<Node> q = new LinkedList<>();
        q.add(new Node(sx, sy));
        while (!q.isEmpty())
        {
            Node t = q.poll();
            int x = t.x;
            int y = t.y;
            int d = dist[x][y];
            for (int i = 0; i < 4; i++)
            {
                int xx = dx[i] + x;
                int yy = dy[i] + y;
                if (xx <= 0 || xx > n || yy <= 0 || yy > n)
                    continue;
                if (st[xx][yy])
                    continue;
                st[xx][yy] = true;
                q.add(new Node(xx, yy));
                dist[xx][yy] = d + 1;
            }
            ArrayList<Node> list = to[x][y];
            for (int i = 0; i < list.size(); i++)
            {
//                int xx = to[x][y].x;
//                int yy = to[x][y].y;
                int xx = list.get(i).x;
                int yy = list.get(i).y;
                if (!st[xx][yy] || xx <= 0 || yy <= 0 || xx > n || yy > n)
                {
                    dist[xx][yy] = d + 1;
                    st[xx][yy] = true;
                    q.add(new Node(xx, yy));
                }
            }
        }
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                to[i][j] = new ArrayList<Node>();

        for (int i = 0; i < m; i++)
        {
            int x = sc.nextInt();
            int y = sc.nextInt();
            int xx = sc.nextInt();
            int yy = sc.nextInt();
            to[x][y].add(new Node(xx, yy));
            to[xx][yy].add(new Node(x, y));

//            to[x][y] = new Node(xx, yy);
//            to[xx][yy] = new Node(x, y);
        }
        bfs(n, n);
        double ans = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                ans += dist[i][j];
        System.out.printf("%.2f", ans / n / n);
    }
}

🍑 hashmap下标映射存传送门版

import java.util.*;

public class Main
{
    static int N = 2020, n, m;
    static int[][] g = new int[N][N];
    static boolean[][] st = new boolean[N][N];
    static ArrayList[][] to = new ArrayList[N][N];
    static int[][] dist = new int[N][N];
    static int[] dx = { -1, 0, 1, 0 };
    static int[] dy = { 0, 1, 0, -1 };

    static class Node
    {
        int x;
        int y;

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

    static void bfs(int sx, int sy)
    {
        st[sx][sy] = true;
        dist[sx][sy] = 0;
        LinkedList<Node> q = new LinkedList<>();
        q.add(new Node(sx, sy));
        while (!q.isEmpty())
        {
            Node t = q.poll();
            int x = t.x;
            int y = t.y;
            int d = dist[x][y];
            for (int i = 0; i < 4; i++)
            {
                int xx = dx[i] + x;
                int yy = dy[i] + y;
                if (xx <= 0 || xx > n || yy <= 0 || yy > n)
                    continue;
                if (st[xx][yy])
                    continue;
                st[xx][yy] = true;
                q.add(new Node(xx, yy));
                dist[xx][yy] = d + 1;
            }
            ArrayList<Node> list = to[x][y];
            for (int i = 0; i < list.size(); i++)
            {
//                int xx = to[x][y].x;
//                int yy = to[x][y].y;
                int xx = list.get(i).x;
                int yy = list.get(i).y;
                if (!st[xx][yy] || xx <= 0 || yy <= 0 || xx > n || yy > n)
                {
                    dist[xx][yy] = d + 1;
                    st[xx][yy] = true;
                    q.add(new Node(xx, yy));
                }
            }
        }
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                to[i][j] = new ArrayList<Node>();

        for (int i = 0; i < m; i++)
        {
            int x = sc.nextInt();
            int y = sc.nextInt();
            int xx = sc.nextInt();
            int yy = sc.nextInt();
            to[x][y].add(new Node(xx, yy));
            to[xx][yy].add(new Node(x, y));

//            to[x][y] = new Node(xx, yy);
//            to[xx][yy] = new Node(x, y);
        }
        bfs(n, n);
        double ans = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                ans += dist[i][j];
        System.out.printf("%.2f", ans / n / n);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值