- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个 4 x 4 网格,即 16 个单元格。每个单元格包含 1,5 之间的值。例如。
0 1 2 3
_ _ _ _
0 - |2|1|3|2|
1 - |1|3|5|1|
2 - |5|2|1|4|
3 - |2|4|2|1|
现在我知道我需要使用 Dijkstra 算法。另外,为了优化这一点,我需要使用优先级队列。
我的目标是找到每个单元格到目的地的最短总和。源在网格上可以是随机的,目的地也可以是随机的。 (即并不总是从左上到右下)。
我曾经处理过使用相邻矩阵的图表。然而,对于这个网格,创建一个相邻矩阵是否明智?即,将所有不相邻的字段设置为无穷大。将单元插入优先级队列的最充分的方法是什么?会是{(行,列),距离}吗?
只是为了澄清我的理解,优先级队列将存储最佳路径?所以细胞和累积距离。因为,Dijstra 的算法使用 BFS,在这种情况下会搜索所有邻居以获得最短距离。
最佳答案
将“节点”设置为具有 3 个字段(行、列、距离)并实现 Comparable 的类。然后使用使用PriorityQueue<Node>
:
import java.util.PriorityQueue;
class Main {
public static void main(String[] args) {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(2, 3, 12));
pq.add(new Node(0, 9, 1));
pq.add(new Node(4, 0, 8));
System.out.println(pq.poll().getDistance()); //prints 1
}
}
class Node implements Comparable<Node>{
private final int row, col, distance;
public Node(int row, int col, int value) {
this.row = row;
this.col = col;
distance = value;
}
int getDistance() {
return distance;
}
@Override
public int compareTo(Node other) {
return Integer.compare(distance, other.distance);
}
}
或者设置 Comperator
到优先级队列所以 Node
不必实现Comparable
:
import java.util.PriorityQueue;
class Main {
public static void main(String[] args) {
PriorityQueue<Node> pq = new PriorityQueue<>(Node::compare);
pq.add(new Node(2, 3, 12));
pq.add(new Node(0, 9, 1));
pq.add(new Node(4, 0, 8));
System.out.println(pq.poll().getDistance()); //prints 1
}
}
class Node{
private final int row, col, distance;
public Node(int row, int col, int value) {
this.row = row;
this.col = col;
distance = value;
}
int getDistance() {
return distance;
}
public static int compare(Node o1, Node o2) {
return Integer.compare(o1.getDistance(), o2.getDistance());
}
}
您还可以使用 lambda 表达式来设置比较器:
PriorityQueue<Node> pq = new PriorityQueue<>((o1,o2)->Integer.compare(o1.getDistance(), o2.getDistance()));
所有三个选项均有效。
关于java - 如何将网格中单元格的邻居存储到优先级队列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61536288/
我从 SpatialPolygonsDataFrame 开始,它包含用于创建加纳各地区 map 的数据(可在 http://www.diva-gis.org/datadown 获取)。我正在尝试创建一
我遇到了一个问题,我需要根据存储在前一个元素中的信息修改容器的元素。示例: 如果前一个 vector 元素可被 2 整除,则将当前元素乘以 10 vector -> [12, 11, 33, 10]
总的来说,我对脚本编写还很陌生。我正在编写一个 expect 脚本,它通过 ssh 进入 Cisco 交换机,并运行“show cdp neighbors”命令来获取连接到交换机的所有设备的列表。然后
我正在尝试比较节点的值。使用 flood-fill 算法,我能够垂直和水平检查网格的每个节点。现在我必须更新我的代码以检查位于对 Angular 线上的单元格,如下图所示: 红色是当前节点,黄色是需要
我使用预先计算的指标使用 Scikit-Learn 的最近邻/半径分类。这意味着,我将成对距离的 n_samples_train x n_samples_train 矩阵传递给分类器的拟合方法。 现在
我有一个大的稀疏图,我将其表示为邻接矩阵(100k x 100k 或更大),存储为边数组。具有(非稀疏)4 x 4 矩阵的示例: 0 7 4 0 example_array = [ [7,1,2],
从有向图中并给出两个顶点 (v, u) 我需要找到:共同的“出”邻居和共同的“入”邻居。 例如: import networkx as nx ghybrid = nx.DiGraph() ghybri
我正在使用 JavaScript 进行图像处理,我想知道是否有任何通用公式可以确定像素的 x 邻居。 我知道对于 3*3 的正方形,可以使用特定的 x 和 y 像素确定 8 个邻居。 (x-1,y-1
在 CentOS 6.4(内核 2.6.32)上,为什么下面的第二个 arping 调用会创建一个新的 ARP 表条目,而第一个不会?网络行为是相同的,我感到困惑的是,在我看来,系统调用实际上是等同的
我是一名优秀的程序员,十分优秀!