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


C++ MapCell::getAStarCost方法代码示例

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


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

示例1: f

list<GameMap::Index2> GameMap::getPath( const Index2& from, const Index2& to )
{
	if (from==to || (to.first<0 || to.first>=m_objects.size() || to.second<0 || to.second>=m_objects[0].size()))
		return list<Index2>();

	struct AStarInfo
	{
		int g;
		int h;
		Index2 parent;
		int f(){ return g+h; }
		AStarInfo(int argg, int argh){
			g = argg;
			h = argh;
			parent.first = parent.second = -1;
		}
	};
	map<Index2, AStarInfo> opened;
	map<Index2, AStarInfo> closed;
	auto last_obj = opened.end();
	opened.insert( make_pair(from, AStarInfo(0,0)) );
	do
	{
		Break_If( opened.empty() );
		int min_cost = INT_MAX;
		Index2 curPos(-1,-1);
		AStarInfo curInfo(INT_MAX, 0);
		map<Index2, AStarInfo>::iterator cur;
		for (map<Index2, AStarInfo>::iterator it = opened.begin(); it != opened.end(); ++it)
		{
			if (it->second.f() < curInfo.f())
			{
				curPos = it->first;
				curInfo = it->second;
				cur = it;
			}
		}
		opened.erase(cur);
		closed.insert( make_pair(curPos, curInfo) );

		auto surround = getSurrounding(curPos, 1);
		for (auto it = surround.begin(); it != surround.end(); ++it)
		{
			MapCell* cell = m_objects[it->first][it->second];
			if ( *it == to )
			{
				AStarInfo info( curInfo.g+loss(curPos,*it), loss(*it, to) );
				info.parent = curPos;
				opened.insert( make_pair(*it, info) );
			}
			else if ( cell->getAStarCost()==INT_MAX || closed.find(*it)!=closed.end() )
			{
				//do nothing
			}
			else if ( opened.find(*it) == opened.end() )
			{
				AStarInfo info( curInfo.g+loss(curPos,*it), loss(*it, to) );
				info.parent = curPos;
				opened.insert( make_pair(*it, info) );
			}
			else//已经在开启列表中,检查新的路径是否更好
			{
				auto has = opened.find(*it);
				if (has->second.g > curInfo.g+loss(curPos,*it))
				{
					has->second.parent = curPos;
					has->second.g = curInfo.g+loss(curPos,*it);
				}
			}
		}
		
		last_obj = opened.find(to);
		if (last_obj != opened.end())
			break;

	} while (true);

	if (last_obj == opened.end())
	{
		return list<Index2>();
	}

	list<Index2> ret;
	ret.push_front( last_obj->first );
	auto path = closed.find( last_obj->second.parent );
	while (path != closed.end())
	{
		ret.push_front(path->first);
		path = closed.find( path->second.parent );
	}
	if (!ret.empty())
		ret.pop_front();

	if (ret.size() > 10)
	{
		ret.clear();
	}
	return ret;
}
开发者ID:gittor,项目名称:Explore,代码行数:99,代码来源:GameMap.cpp


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