コード例 #1
0
ファイル: AStar.cpp プロジェクト: thibetabc/Game2048
void AStar::Execute(const Graph &Graph, const string &VetexId)
{
	const auto& Vertexes = Graph.GetVertexes();
	Vertex* pVertexStart = Vertexes.find(VetexId)->second;
	vector< Vertex* > Q;

	// 初始化顶点  
	for (auto& it : Vertexes)
	{
		Vertex* pV = it.second;

		pV->PathfindingData.Cost = 0;
		pV->PathfindingData.pParent = nullptr;
		pV->PathfindingData.Heuristic = 0x0FFFFFFF;
		pV->PathfindingData.Flag = false;
	}

	// 初始化起始顶点  
	pVertexStart->PathfindingData.pParent = 0;
	pVertexStart->PathfindingData.Cost = 0;
	pVertexStart->PathfindingData.Heuristic = Estimate(pVertexStart, m_pVTarget);

	// 把起始顶点放入列表中  
	Q.push_back(pVertexStart);
	pVertexStart->PathfindingData.Flag = true;

	for (; Q.size() > 0;)
	{
		// 选出最小路径估计的顶点  
		auto v = ExtractMin(Q);
		v->PathfindingData.Flag = false;
		if (v == m_pVTarget)
		{
			return;
		}
		// 对所有的出边进行“松弛”  
		const auto& EO = v->GetEdgesOut();
		for (auto& it : EO)
		{
			Edge* pEdge = it.second;
			Vertex* pVEnd = pEdge->GetEndVertex();

			bool bRet = Relax(v, pVEnd, pEdge->GetWeight());
			// 如果松弛成功,加入列表中
			if (bRet && pVEnd->PathfindingData.Flag == false)
			{
				Q.push_back(pVEnd);
				pVEnd->PathfindingData.Flag = true;
			}
		}
	}
}
コード例 #2
0
ファイル: Dijkstra.cpp プロジェクト: thibetabc/Game2048
void Dijkstra::Execute(const Graph& graph, const string& vetexId)
{
	m_Ret.PathTree.clear();
	const auto &Vertexes = graph.GetVertexes();
	Vertex* pVertexStart = Vertexes.find(vetexId)->second;
	vector< Vertex* > Q;
	//// 初始化   
	//for (auto& it : Vertexes)
	//{
	//	it.second->PathfindingData.Cost = 0x0FFFFFFF;
	//	Q.push_back(it.second);
	//	pVertexStart->PathfindingData.pParent = nullptr;
	//}
	////m_Ret.PathTree[pVertexStart] = 0;    //  起始顶点的前驱顶点为空  
	//pVertexStart->PathfindingData.Cost = 0;
	//for (; Q.size() > 0;)
	//{
	//	// 选出最小路径估计的顶点   
	//	auto v = ExtractMin(Q);
	//	// 对所有的出边进行“松弛”   
	//	const auto& EO = v->GetEdgesOut();
	//	for (auto& it : EO)
	//	{
	//		Edge* pEdge = it.second;
	//		Relax(v, pEdge->GetEndVertex(), pEdge->GetWeight());
	//	}
	//}

	// 初始化   
	for (auto& it : Vertexes)
	{
		it.second->PathfindingData.Cost = 0x0FFFFFFF;
		pVertexStart->PathfindingData.pParent = nullptr;
	}

	// 初始化起始顶点
	//m_Ret.PathTree[pVertexStart] = 0;    //  起始顶点的前驱顶点为空  
	pVertexStart->PathfindingData.Cost = 0;
	pVertexStart->PathfindingData.pParent = nullptr;

	// 把起始顶点放入列表中  
	Q.push_back(pVertexStart);
	pVertexStart->PathfindingData.Flag = true;

	for (; Q.size() > 0;)
	{
		// 选出最小路径估计的顶点   
		auto v = ExtractMin(Q);
		v->PathfindingData.Flag = false;

		// 对所有的出边进行“松弛”   
		const auto& EO = v->GetEdgesOut();
		for (auto& it : EO)
		{
			Edge* pEdge = it.second;
			Vertex* pVEnd = pEdge->GetEndVertex();

			bool bRel = Relax(v, pVEnd, pEdge->GetWeight());
			if (bRel && pVEnd->PathfindingData.Flag == false)
			{
				Q.push_back(pVEnd);
				pVEnd->PathfindingData.Flag = true;
			}
		}
	}
}