A 寻路算法模拟实现 C++ 可运行.docVIP

  • 9
  • 0
  • 约4.9千字
  • 约 6页
  • 2016-02-24 发布于江苏
  • 举报
A 寻路算法模拟实现 C++ 可运行.doc

////////////////////////////////////////////////////////////////////////// // // // // //写一个自己实现的A*搜索算法 ////////////////////////////////////////////////////////////////////////// #include stdafx.h #include iostream #include vector #include map #include algorithm #include assert.h using namespace std; const int nMapWidth = 8; const int nMapHeight = 8; struct Node { int nEnable; int nNodeMark; int nValue; int x; int y; Node():nEnable(0),nNodeMark(0),nValue(0),x(0),y(0){}; }; std::mapint ,int m_OpenList; std::mapint ,int m_CloseList; std::vectorint m_KeyList; Node m_MapNode[nMapWidth][nMapHeight]; //计算openlist中靠前节点周围的节点 void ComputerRound(int curx,int cury); //将一个新的节点加入到OPenList中 void AddNodeToOpenList(Node* pNode,int nNum); //打印地图 void Print(Node pNode[][nMapHeight]); void Print(Node pNode[][nMapHeight]) { for (int n = 0; n nMapWidth; ++n) { for(int m = 0; m nMapHeight; ++m) { if (m == 0) coutendl; if (n == 0) coutpNode[n][m].nEnable/*( pNode[n][m].nNodeMark)*/ ; else coutpNode[n][m].nEnable/*(pNode[n][m].nNodeMark)*/ ; } } } //将一个新的节点加入到OPenList中 void AddNodeToOpenList(Node* pNode,int nNum) { if(!pNode || !(pNode-nEnable)) return; if (m_OpenList.empty()) { m_OpenList[pNode-nNodeMark] = nNum; m_KeyList.push_back(pNode-nNodeMark); } else { std::mapint,int::iterator itr = m_OpenList.find(pNode-nNodeMark); if (itr == m_OpenList.end()) { std::mapint,int::iterator itrQ = m_CloseList.find(pNode-nNodeMark); if (itrQ != m_CloseList.end()) { if (nNum (*itrQ).second) { m_CloseList.erase(itrQ); } else return; } else { m_OpenList[pNode-nNodeMark] =nNum; m_KeyList.push_back(pNode-nNodeMark); } } else { if ((*itr).second nNum) { m_OpenList[pNode-nNodeMark] =nNum; } } } } //将openlist中的一个靠前的节点展开到CloseList中 void AddNodeToCloseList(Node* pNode,int nNum) { if(!pNode) return; if (m_CloseList.empty()) { m_CloseList[pNode-nNodeMark] = nNum;

文档评论(0)

1亿VIP精品文档

相关文档