当前位置: 首页>>代码示例>>Java>>正文


Java MinPQ.insert方法代码示例

本文整理汇总了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);
}
 
开发者ID:SkullTech,项目名称:algorithms-princeton,代码行数:16,代码来源:Solver.java

示例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); }
}
 
开发者ID:SkullTech,项目名称:algorithms-princeton,代码行数:28,代码来源:Solver.java

示例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();
    }
}
 
开发者ID:SkullTech,项目名称:algorithms-princeton,代码行数:13,代码来源:TopM.java

示例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));
    		}
		}	
	}
}
 
开发者ID:Lxinyuelxy,项目名称:Princeton_Algorithms,代码行数:52,代码来源:Solver.java


注:本文中的edu.princeton.cs.algs4.MinPQ.insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。