本文整理汇总了Java中edu.princeton.cs.algs4.MinPQ.insert方法的典型用法代码示例。如果您正苦于以下问题:Java MinPQ.insert方法的具体用法?Java MinPQ.insert怎么用?Java MinPQ.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.princeton.cs.algs4.MinPQ
的用法示例。
在下文中一共展示了MinPQ.insert方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Solver
import edu.princeton.cs.algs4.MinPQ; //导入方法依赖的package包/类
public Solver(Board initial) {
if (initial == null) throw new IllegalArgumentException();
MinPQ<SearchNode> pq = new MinPQ<>();
pq.insert(new SearchNode(initial, 0, null));
Board twin = initial.twin();
MinPQ<SearchNode> tpq = new MinPQ<>();
tpq.insert(new SearchNode(twin, 0, null));
SolutionObj sol = solve(pq, tpq);
this.moves = sol.moves;
this.isSolvable = sol.isSolvable;
this.solution = buildSolution(sol.solution);
}
示例2: solve
import edu.princeton.cs.algs4.MinPQ; //导入方法依赖的package包/类
private SolutionObj solve(MinPQ<SearchNode> pq, MinPQ<SearchNode> tpq) {
SearchNode node = pq.min(), tnode = pq.min();
while(!node.board.isGoal() || !tnode.board.isGoal()) {
node = pq.delMin();
tnode = tpq.delMin();
for (Board neighbor: node.board.neighbors()) {
if (neighbor.isGoal()) {
node = new SearchNode(neighbor, node.moves+1, node);
return new SolutionObj(node, node.moves, true);
}
else if (node.previous == null || !node.previous.board.equals(neighbor))
pq.insert(new SearchNode(neighbor, node.moves+1, node));
}
for (Board tneighbor: tnode.board.neighbors()) {
if (tneighbor.isGoal()) {
return new SolutionObj(null, -1, false);
}
else if (tnode.previous == null || !tnode.previous.board.equals(tneighbor))
tpq.insert(new SearchNode(tneighbor, tnode.moves+1, tnode));
}
}
if (node.board.isGoal()) { return new SolutionObj(node, node.moves, true); }
else { return new SolutionObj(null, -1, false); }
}
示例3: main
import edu.princeton.cs.algs4.MinPQ; //导入方法依赖的package包/类
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
MinPQ<Transaction> pq = new MinPQ<>();
while (StdIn.hasNextLine()) {
String line = StdIn.readLine();
Transaction item = new Transaction(line);
pq.insert(item);
if (pq.size() > M) pq.delMin();
}
}
示例4: Solver
import edu.princeton.cs.algs4.MinPQ; //导入方法依赖的package包/类
public Solver(Board initial) {
MinPQ<Node> minPQ = new MinPQ<Node>();
MinPQ<Node> minPQTwin = new MinPQ<Node>();
minPQ.insert(new Node(initial, 0, null));
minPQTwin.insert(new Node(initial.twin(), 0, null));
resBoards.enqueue(initial);
if(initial.isGoal()) {
isSolverable = true;
return;
}
else if(initial.twin().isGoal()){
isSolverable = false;
return;
}
while(true){
Node node = minPQ.delMin();
if(node.board.isGoal()) {
isSolverable = true;
while(node.previous != null) {
node = node.previous;
resBoards.enqueue(node.board);
}
return;
}else{
node.move++;
Iterable<Board> neighbors = node.board.neighbors();
for(Board b : neighbors) {
if(node.previous != null && b.equals(node.previous.board));
else
minPQ.insert(new Node(b, node.move, node));
}
}
Node nodeTwin = minPQTwin.delMin();
if(nodeTwin.board.isGoal()){
isSolverable = false;
return;
}
else{
nodeTwin.move++;
Iterable<Board> neighborsTwin = nodeTwin.board.neighbors();
for(Board bt : neighborsTwin){
if(nodeTwin.previous != null && bt.equals(nodeTwin.previous.board));
else
minPQTwin.insert(new Node(bt, nodeTwin.move++, nodeTwin));
}
}
}
}