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


C++ PriorityQueue::remove_element方法代码示例

本文整理汇总了C++中PriorityQueue::remove_element方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::remove_element方法的具体用法?C++ PriorityQueue::remove_element怎么用?C++ PriorityQueue::remove_element使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue::remove_element方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: if

bool Hex<T>::shortest_path(T source, T target, player_t player)
{
	PriorityQueue<T,T> priorityQueue;
	// Apart from board positions, we also add source and target in the priority queue.
	// source & target: These are virtual home and goal.

	//Initialize cost of vertices as infinite except the source vertex
	for (T vtx_i = 0; vtx_i != graph_.nVertex(); ++vtx_i)
	{
		vecVisited_[vtx_i] = false;
		if (vtx_i == source)
		{
			vecCost_[vtx_i] = 0;
			priorityQueue.insert_element(vtx_i,0);
		} 
		else
		{
			vecCost_[vtx_i] = std::numeric_limits<T>::max();

			if ( ( is_pos_on_board(vtx_i) && (board_.position_player(vtx_i) == player) ) ||
				(vtx_i == target) )
			{
				priorityQueue.insert_element(vtx_i,std::numeric_limits<T>::max());
			}
		}
	}

	bool reachedTarget = false;
	while (!priorityQueue.empty())
	{
		std::pair<T,T> topElem = priorityQueue.top();
		T vtxTop = topElem.first;
		T costTop = topElem.second;
		vecVisited_[vtxTop] = true;
		priorityQueue.remove_element(vtxTop,costTop);

		if (costTop == std::numeric_limits<T>::max())
		{
			break; // reaching here means path from source to target is not present
		}
		if (vtxTop == target)
		{
			reachedTarget = true;
			break;
		}

		std::vector<T> neighbors = graph_.neighbors(vtxTop);
		for (std::vector<T>::iterator it = neighbors.begin(); it != neighbors.end(); ++it)
		{
			if (vecVisited_[*it])
			{
				continue;
			}
			// For a board position consider this neighbor only if the current player has placed
			// his/her hex on this position.
			
			if (is_pos_on_board(*it))
			{
				if (board_.position_player(*it) != player)
				{
					continue;
				}
			}
			else if ((*it) != target)
			{
				continue;
			}

			T newCost = vecCost_[vtxTop] + 1;
			if (newCost < vecCost_[*it])
			{
				priorityQueue.change_priority(*it,vecCost_[*it],newCost);
				vecCost_[*it] = newCost;
			}
		}
	}

	return reachedTarget;
}
开发者ID:kaushikacharya,项目名称:course_programming_assignment,代码行数:79,代码来源:Hex.hpp


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