代码随想录训练营第62天

Floyd 算法精讲 

Floyd 算法代码很简单,但真正理解起原理 还是需要花点功夫,大家在看代码的时候,会发现 Floyd 的代码很简单,甚至看一眼就背下来了,但我为了讲清楚原理,本篇还是花了大篇幅来讲解。

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[][][] grid = new int[n + 1][n + 1][n + 1]; // Java数组初始化为0
        for (int i = 0; i < m; i++) {
            int p1 = scanner.nextInt();
            int p2 = scanner.nextInt();
            int val = scanner.nextInt();
            grid[p1][p2][0] = val;
            grid[p2][p1][0] = val; // 注意这里是双向图
        }

        // 开始 floyd
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    grid[i][j][k] = Math.min(grid[i][j][k - 1], grid[i][k][k - 1] + grid[k][j][k - 1]);
                }
            }
        }

        // 输出结果
        int z = scanner.nextInt();
        while (z-- > 0) {
            int start = scanner.nextInt();
            int end = scanner.nextInt();
            if (grid[start][end][n] == 10005) {
                System.out.println(-1);
            } else {
                System.out.println(grid[start][end][n]);
            }
        }
        scanner.close();
    }
}

代码随想录

A * 算法精讲 (A star算法)

一般 笔试或者 面试的时候,不会考察A*, 都是会结合具体业务场景问 A*算法,例如:地图导航,游戏开发 等等。

其实基础版的A* 并不难,所以大家不要畏惧,理解本篇内容,甚至独立写出代码,大家可以做到,加油 

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    static int[][] moves = new int[1001][1001];
    static int[][] dir = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}};
    static int b1, b2;

    static class Knight {
        int x, y;
        int g, h, f;

        public Knight(int x, int y, int g, int h) {
            this.x = x;
            this.y = y;
            this.g = g;
            this.h = h;
            this.f = g + h;
        }

        // 自定义比较器,用于优先队列排序
        public static final Comparator<Knight> comparator = new Comparator<Knight>() {
            @Override
            public int compare(Knight k1, Knight k2) {
                return Integer.compare(k1.f, k2.f);
            }
        };
    }

    public static int Heuristic(Knight k) {
        return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2); // 欧几里得距离的平方
    }

    public static void astar(Knight start) {
        PriorityQueue<Knight> que = new PriorityQueue<>(Knight.comparator);
        que.add(start);

        while (!que.isEmpty()) {
            Knight cur = que.poll();
            if (cur.x == b1 && cur.y == b2) {
                break;
            }
            for (int i = 0; i < dir.length; i++) {
                Knight next = new Knight(cur.x + dir[i][0], cur.y + dir[i][1], cur.g + 5, Heuristic(new Knight(cur.x + dir[i][0], cur.y + dir[i][1], 0, 0)));
                if (next.x >= 1 && next.x <= 1000 && next.y >= 1 && next.y <= 1000 && moves[next.x][next.y] == 0) {
                    moves[next.x][next.y] = moves[cur.x][cur.y] + 1;
                    que.add(next);
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n;
        while (scanner.hasNextInt()) {
            n = scanner.nextInt();
            while (n-- > 0) {
                int a1 = scanner.nextInt();
                int a2 = scanner.nextInt();
                b1 = scanner.nextInt();
                b2 = scanner.nextInt();
                Arrays.fill(moves[0], 0);
                astar(new Knight(a1, a2, 0, Heuristic(new Knight(a1, a2, 0, 0))));
                System.out.println(moves[b1][b2]);
            }
        }
        scanner.close();
    }
}

A * 算法精讲 (A star算法) | 代码随想录

最短路算法总结篇

最各个最短路算法有个全面的了解

最短路算法总结篇 | 代码随想录

图论总结 

图论总结篇 | 代码随想录

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值