当前位置: 首页 > news >正文

代码随想录Day76(图论Part11)

97.小明逛公园(Floyd)

题目:97. 小明逛公园 (kamacoder.com)

思路:

答案
import java.util.*;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];final int INF = 10005; // 因为边的最大距离是10^4// 初始化图的邻接矩阵for (int i = 1; i <= n; i++) {Arrays.fill(grid[i], INF);grid[i][i] = 0; // 自己到自己的距离为0}// 读取边的信息并填充邻接矩阵for (int i = 0; i < m; i++) {int p1 = scanner.nextInt();int p2 = scanner.nextInt();int val = scanner.nextInt();grid[p1][p2] = val;grid[p2][p1] = val; // 注意这里是双向图}// 开始 Floyd-Warshall 算法for (int k = 1; k <= n; k++) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {grid[i][j] = Math.min(grid[i][j], grid[i][k] + grid[k][j]);}}}// 读取查询并输出结果int z = scanner.nextInt(); // 查询次数while (z-- > 0) {int start = scanner.nextInt();int end = scanner.nextInt();if (grid[start][end] == INF) {System.out.println(-1);} else {System.out.println(grid[start][end]);}}scanner.close();}
}
小结

126.骑士的攻击(A*算法)

题目:126. 骑士的攻击 (kamacoder.com)

思路:毫无疑问是最难的一题

答案
import java.util.*;class Knight implements Comparable<Knight> {int x, y, 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;}@Overridepublic int compareTo(Knight k) {return Integer.compare(this.f, k.f); // 从小到大排序}
}public class Main {static int[][] moves = new int[1001][1001];static int[][] dir = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2},{2, 1}, {2, -1}, {1, -2}, {-1, -2}};static int b1, b2;static PriorityQueue<Knight> que = new PriorityQueue<>();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) {que.add(start);while (!que.isEmpty()) {Knight cur = que.poll();if (cur.x == b1 && cur.y == b2)break;for (int i = 0; i < 8; i++) {int nextX = cur.x + dir[i][0];int nextY = cur.y + dir[i][1];if (nextX < 1 || nextX > 1000 || nextY < 1 || nextY > 1000)continue;if (moves[nextX][nextY] == 0) {moves[nextX][nextY] = moves[cur.x][cur.y] + 1;int nextG = cur.g + 5; // 马走日,1 * 1 + 2 * 2 = 5int nextH = heuristic(new Knight(nextX, nextY, 0, 0));que.add(new Knight(nextX, nextY, nextG, nextH));}}}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();while (n-- > 0) {int a1 = scanner.nextInt();int a2 = scanner.nextInt();b1 = scanner.nextInt();b2 = scanner.nextInt();for (int[] row : moves) Arrays.fill(row, 0);Knight start = new Knight(a1, a2, 0, heuristic(new Knight(a1, a2, 0, 0)));astar(start);que.clear(); // 清空队列System.out.println(moves[b1][b2]);}scanner.close();}
}

http://www.lryc.cn/news/391388.html

相关文章:

  • 工程化:Commitlint / 规范化Git提交消息格式
  • 电脑有线网卡和无线网卡的MAC地址
  • 代码随想录-DAY②-数组——leetcode 977 | 209
  • 稀疏数组搜索
  • 存储器类型介绍
  • 论文学习笔记1:Federated Graph Neural Networks: Overview, Techniques, and Challenges
  • [数据集][目标检测]轮椅检测数据集VOC+YOLO格式13826张1类别
  • 视频剪辑音乐自动卡点Pr插件 BeatEdit v2.2 免费下载
  • 【INTEL(ALTERA)】为什么Nios® II构建流程报告无法在 Windows WSL 上确定程序大小?
  • 2024年第十四届APMCM亚太地区大学生数学建模竞赛
  • 删除账户相关信息
  • JavaSE (Java基础):面向对象(下)
  • Element中的日期时间选择器DateTimePicker和级联选择器Cascader
  • Construct公司 从 0 到 1 基于 Kitex+Istio 的微服务系统建设
  • day04-组织架构
  • Web3 开发者入门手册:技能、工具和职业前景
  • 元宇宙虚拟实景展馆树立客户对企业的信任和好感
  • 【C语言】宏定义在 a.c 中定义,如何在 b.c 中使用?
  • vue3 滚动条滑动到元素位置时,元素加载
  • [Linux] 相对路径(Relative Path)与绝对路径(Absolute Path)
  • [ESP32] I2S播放wav文件
  • YOLOv8
  • 协程调度模块
  • 2024 最新docker仓库镜像,6月,7月
  • 探索Vim的文本处理能力:精通查找与替换
  • 2024.7.4学习日报
  • 享元模式(Flyweight Pattern)
  • Oracle连接mysql
  • golang 垃圾回收
  • React 中如何使用 Monaco