• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java MinPQ类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中edu.princeton.cs.algs4.MinPQ的典型用法代码示例。如果您正苦于以下问题:Java MinPQ类的具体用法?Java MinPQ怎么用?Java MinPQ使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



MinPQ类属于edu.princeton.cs.algs4包,在下文中一共展示了MinPQ类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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: solveAStar

import edu.princeton.cs.algs4.MinPQ; //导入依赖的package包/类
private boolean solveAStar(SearchNode init) {
/* Solve the sliding puzzle game using the A* algorithm:
 * Put the root node in the priority queue. Then, while the solution
 * isn't found:
 * -> dequeue the node with the minimum priority (calculated with manhattan / hamming)
 * -> check if the node is the goal node, if it is, return the final path.
 * -> if it's not, enqueue every neighbor if the current node in the pq
 *    if the neighbor is different than the current node.
 * -> return to the loop
 */
    SearchNode sn;
    boolean watch;
    pq = new MinPQ<>();
    pq.insert(init);
    moves = 0;
    while (true) {
        sn = pq.delMin();
        if(sn.b.isGoal())
            return createPath(sn);
        watch = true;
        for(Board neighB : sn.b.neighbors()) {
            if(watch && neighB.equals(sn.b))
                watch = false;
            else
                pq.insert(new SearchNode(neighB, sn.nMovs + 1, sn));
        }
    }
}
 
开发者ID:robotenique,项目名称:intermediateProgramming,代码行数:29,代码来源:Solver.java


示例5: 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类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java AbstractContentReader类代码示例发布时间:2022-05-22
下一篇:
Java PersistentEntityInformation类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap