Skip to content

Commit

Permalink
add the method of calculate the steps to start node
Browse files Browse the repository at this point in the history
  • Loading branch information
linkliu committed Aug 18, 2024
1 parent 4202a88 commit 6041021
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 18 deletions.
4 changes: 2 additions & 2 deletions algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ Map& Algorithm::GetMap()
return aMap;
}

MNode& Algorithm::GetStartNode()
const MNode& Algorithm::GetStartNode() const
{
return startNode;
}

MNode& Algorithm::GetEndNode()
const MNode& Algorithm::GetEndNode() const
{
return endNode;
}
4 changes: 2 additions & 2 deletions algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Algorithm

protected:
Map& GetMap();
MNode& GetStartNode();
MNode& GetEndNode();
const MNode& GetStartNode() const;
const MNode& GetEndNode() const;

public:
Algorithm() = default;
Expand Down
2 changes: 1 addition & 1 deletion astar_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class AStarAlgorithm : public Algorithm
AStarAlgorithm(Map& _map):Algorithm(_map){}
AStarAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){}
map<int, int> Resolve() override;
vector<int> FindPath() override;
vector<int> FindPath(map<int, int>&) override;
};
#endif
58 changes: 50 additions & 8 deletions bfs_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ using std::make_pair;
map<int, int> BFSAlgorithm::Resolve()
{
Map& aMap = GetMap();
MNode& startNode = GetStartNode();
MNode& endNode = GetEndNode();
const MNode& startNode = GetStartNode();
const MNode& endNode = GetEndNode();
map<int, int> solveMap;
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size() < 0)
{
Expand Down Expand Up @@ -50,11 +50,11 @@ map<int, int> BFSAlgorithm::Resolve()
}


vector<int> BFSAlgorithm::FindPath(map<int, int>& oriMap)
vector<int> BFSAlgorithm::FindPath(map<int, int>& solveMap)
{
Map& aMap = GetMap();
MNode& startNode = GetStartNode();
MNode& endNode = GetEndNode();
const Map& aMap = GetMap();
const MNode& startNode = GetStartNode();
const MNode& endNode = GetEndNode();
vector<int> pathVec;
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size()<=0)
{
Expand All @@ -65,10 +65,10 @@ vector<int> BFSAlgorithm::FindPath(map<int, int>& oriMap)
return pathVec;
}
int checkNodeNum = aMap.GetNodeNum(endNode);
while (oriMap.find(checkNodeNum) != oriMap.cend())
while (solveMap.find(checkNodeNum) != solveMap.cend())
{
pathVec.push_back(checkNodeNum);
checkNodeNum = oriMap.find(checkNodeNum)->second;
checkNodeNum = solveMap.find(checkNodeNum)->second;
}
//有路径
if(checkNodeNum == aMap.GetNodeNum(startNode))
Expand All @@ -82,3 +82,45 @@ vector<int> BFSAlgorithm::FindPath(map<int, int>& oriMap)
return pathVec;
}

int BFSAlgorithm::CalStepsToStart(const MNode& cnode, const map<int, int>& solveMap)
{
Map& aMap = GetMap();
if(!aMap.NodeCheck(cnode) || solveMap.size() <= 0)
{
stringstream ss;
ss<<"invalid cnode:"<<cnode.ToString()<<", or solveMap.size=:"<<solveMap.size()<<endl;
cout<<ss.str();
endwin();
return -1;
}
const MNode& startNode = GetStartNode();
int checkNodeNum = aMap.GetNodeNum(cnode);
int step = 0;
while (solveMap.find(checkNodeNum) != solveMap.cend())
{
step+=1;
checkNodeNum = solveMap.find(checkNodeNum)->second;
}
return step;
}

void BFSAlgorithm::DrawNodeSteps(const map<int, int>& solveMap)
{
Map& aMap = GetMap();
const MNode& startNode = GetStartNode();
const MNode& endNode = GetEndNode();
for (int firdex = 0; firdex < aMap.Size(); firdex++)
{
pair<int, int> firPair = aMap.ExchNumToMapIndex(firdex);
MNode& node = aMap.GetNode(firPair.first, firPair.second);
int steps = CalStepsToStart(node, solveMap);
if(node != startNode && node != endNode && aMap.Reacheable(node))
{
aMap.Draw(steps, firPair.first, firPair.second);
}
}
refresh();
}



3 changes: 3 additions & 0 deletions bfs_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _BFS_ALGORITHM_H
#include "algorithm.h"
#include "map.h"
#include "mnode.h"
#include <map>
#include <vector>
using std::vector;
Expand All @@ -13,5 +14,7 @@ class BFSAlgorithm : public Algorithm
BFSAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){}
map<int, int> Resolve() override;
vector<int> FindPath(map<int, int>&) override;
int CalStepsToStart(const MNode&, const map<int, int>&);
void DrawNodeSteps(const map<int, int>& solveMap);
};
#endif
2 changes: 1 addition & 1 deletion dijstra_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class DIJAlgorithm : public Algorithm
DIJAlgorithm(Map& _map):Algorithm(_map){}
DIJAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){}
map<int, int> Resolve() override;
vector<int> FindPath() override;
vector<int> FindPath(map<int, int>&) override;
};
#endif
5 changes: 3 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ int main(int argc, char* argv[])
BFSAlgorithm bfs(tMap, startNode, endNode);
map<int, int> bfsSolveMap = bfs.Resolve();
// tMap.DrawOriginPath(bfsSolveMap, startNode, endNode);
vector<int> pathVec = bfs.FindPath(bfsSolveMap);
tMap.DrawFinalPath(pathVec, startNode, endNode);
// vector<int> pathVec = bfs.FindPath(bfsSolveMap);
// tMap.DrawFinalPath(pathVec, startNode, endNode);
bfs.DrawNodeSteps(bfsSolveMap);
refresh();
getch();
tMap.ClearMap();
Expand Down
7 changes: 6 additions & 1 deletion map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool Map::isNumValid(int num) const
return num >= 0 && num < Size();
}

bool Map::NodeCheck(const MNode &node)
bool Map::NodeCheck(const MNode &node) const
{
return isMapIndexValid(node.mapIndex_Y, node.mapIndex_X);
}
Expand Down Expand Up @@ -339,6 +339,11 @@ bool Map::isReachable(const MNode &node) const
return false;
}

bool Map::Reacheable(const MNode& node) const
{
return isReachable(node);
}

MNode& Map::GetNode(int _mapIndex_Y, int _mapIndex_X)
{
if(isMapIndexValid(_mapIndex_Y, _mapIndex_X))
Expand Down
3 changes: 2 additions & 1 deletion map.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ class Map
pair<int, int> ExchMapIndexToPOS(int _mapIndex_Y, int _mapIndex_X) const;
int ExchMapIndexToNum(int _mapIndex_Y, int _mapIndex_X) const;
list<MNode> GetNeighbors(const MNode& node);
bool NodeCheck(const MNode& node);
bool NodeCheck(const MNode& node) const;
void DrawTerrain(const map<ENodeType, list<pair<int, int>>>& terMap);
void DrawOriginPath(const map<int, int> & oriMap, const MNode&, const MNode&);
void DrawFinalPath(const vector<int>& pathVec, const MNode&, const MNode&);
bool Reacheable(const MNode&) const;
};
#endif

0 comments on commit 6041021

Please sign in to comment.