本文整理汇总了Java中java.util.PriorityQueue.contains方法的典型用法代码示例。如果您正苦于以下问题:Java PriorityQueue.contains方法的具体用法?Java PriorityQueue.contains怎么用?Java PriorityQueue.contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.contains方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shortestPath
import java.util.PriorityQueue; //导入方法依赖的package包/类
public void shortestPath(int source, int destination) {
Set<Node> visited = new HashSet<>();
PriorityQueue<Node> pQueue = new PriorityQueue<>(new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.cost - o2.cost;
}
});
nodes[source].cost = 0;
pQueue.add(nodes[source]);
while(!pQueue.isEmpty()) {
Node currVertex = pQueue.poll();
for(int i = 0; i < graph.length; i++) {
if(graph[currVertex.id][i]!=0 && !visited.contains(nodes[i]) ) {
if(!pQueue.contains(nodes[i])) {
nodes[i].cost = currVertex.cost + graph[currVertex.id][i];
nodes[i].parent = currVertex;
pQueue.add(nodes[i]);
}
else {
nodes[i].cost = Math.min(nodes[i].cost, currVertex.cost + graph[currVertex.id][i]);
if(nodes[i].cost == currVertex.cost + graph[currVertex.id][i])
nodes[i].parent = currVertex;
}
}
}
visited.add(currVertex);
}
}
示例2: findPathTo
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
* Precondition: the unit has enough mobility to reach the Terrain that the path is being found to.
* The returned path will be optimal, uses A*
*
* @return a queue of Terrains that could be traversed to reach the destination
* @throws Exception
*/
private Queue<Terrain> findPathTo(Terrain target) throws Exception {
Queue<Terrain> ans = new LinkedList<>();
if (null == target) {
return new LinkedList<>();
}
if (target == getLocation()) {
//you are already here
return ans;
}
Map<Terrain, Terrain> previousLink = new HashMap<>();
previousLink.put((Terrain) getLocation(), null);
@SuppressWarnings({"unchecked", "rawtypes"})
Comparator<Terrain> comp = new Comparator() {
@Override
/**
* Approximates which path is better
*/
public int compare(Object o1, Object o2) {
Terrain t1 = (Terrain) o1;
Terrain t2 = (Terrain) o2;
// Integer c1 = totalCost(savedPaths.get(t1))+t1.getDistanceTo(target);
// Integer c2 = totalCost(savedPaths.get(t2))+t2.getDistanceTo(target);
Integer c1 = totalCost(t1, previousLink) + t1.getDistanceTo(target);
Integer c2 = totalCost(t2, previousLink) + t2.getDistanceTo(target);
return c1.compareTo(c2);
}
};
PriorityQueue<Terrain> toCheck = new PriorityQueue<>(comp);
toCheck.add((Terrain) getLocation());
while (!toCheck.isEmpty()) {
Terrain current = toCheck.poll();
for (Terrain t : current.getAllAdjacentTerrains()) {
if (999 != t.getMoveCost(getMovementType()) &&
(null == getGrid().get(t) || ((Unit) (getGrid().get(t))).getOwner() == this.getOwner())) {
boolean updated = false;
if (!previousLink.containsKey(t)) {
previousLink.put(t, current);
updated = true;
}
if (t == target) {
return genPath(t, previousLink);
}
if (t.getMoveCost(getMovementType()) + totalCost(current, previousLink) < totalCost(t, previousLink)) {
previousLink.put(t, current);
updated = true;
}
if (totalCost(t, previousLink) <= getCurrentMobility() && updated) {
if (!toCheck.contains(t)) {
toCheck.add(t);
}
}
}
}
}
//if you got here, then there is no path
//See Precondition: there is a path, checked by getValidMoveSpaces
throw new Exception("no path, precondition not met");
}