-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
205 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef _algorithm_h | ||
#define _algorithm_h | ||
#include "map.h" | ||
#include "mnode.h" | ||
#include <map> | ||
class Algorithm | ||
{ | ||
private: | ||
Map aMap; | ||
MNode startNode; | ||
MNode endNode; | ||
|
||
public: | ||
Algorithm() = default; | ||
Algorithm(Map& _map):aMap(_map){} | ||
Algorithm(Map& _map, MNode& _start, MNode& _end):aMap(_map),startNode(_start), endNode(_end){} | ||
virtual void Resolve() = 0; | ||
virtual void FindPath() = 0; | ||
virtual ~Algorithm() = default; | ||
|
||
|
||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +0,0 @@ | ||
#include <algorithm> | ||
#include <chrono> | ||
#include <curses.h> | ||
#include <iostream> | ||
#include <list> | ||
#include <sstream> | ||
#include <thread> | ||
#include <vector> | ||
#include "mnode.h" | ||
#include "debug_tool.h" | ||
#include "astar_algorithm.h" | ||
using std::cout; | ||
using std::endl; | ||
using std::stringstream; | ||
using std::vector; | ||
using std::find_if; | ||
using std::make_pair; | ||
|
||
static map<ENodeType, list<pair<int,int>>> ElementsMap = | ||
{ | ||
{ENodeType::BARRIER, | ||
{ | ||
{13,4},{14,4},{15,4}, {15,4},{16,4}, {17,4},{18,4},{19,4}, | ||
{13,5},{14,5},{15,5}, {15,5},{16,5}, {17,5},{18,5},{19,5}, | ||
{0,4},{1,4},{2,4}, {3,4},{4,4}, {5,4},{6,4},{7,4},{8,4},{9,4},{10,4},{11,4},{11,5},{11,6},{11,7},{11,8},{11,9},{11,10},{11,11}, | ||
{0,5},{1,5},{2,5}, {3,5},{4,5}, {5,5},{6,5},{7,5}, | ||
} | ||
}, | ||
{ENodeType::WATER, | ||
{ | ||
{2,12},{3,12},{4,12},{5,12},{6,12},{7, 12}, {8,12}, {9,12},{10,12},{11,12},{12,12}, | ||
{2,13},{3,13},{4,13},{5,13},{6,13},{7, 13}, {8,13}, {9,13},{10,13},{11,13},{12,13} | ||
} | ||
} | ||
}; | ||
|
||
void ConstructMap(Map &aMap) | ||
{ | ||
if(aMap.Size() > 0) | ||
{ | ||
//cout<<endl; | ||
//cout<<"constsruct--------------------------------------------------start"<<endl; | ||
for (pair<ENodeType, list<pair<int, int>>> ePair : ElementsMap) | ||
{ | ||
for (pair<int, int> miPair : ePair.second) | ||
{ | ||
MNode& node = aMap.GetNode(miPair.first, miPair.second); | ||
node.NTypeSetter(ePair.first); | ||
//cout<<node.NType<<"$->"<<node.str<<","; | ||
aMap.Draw(node, EDrawType::TYPE); | ||
} | ||
} | ||
//cout<<endl; | ||
//cout<<"constsruct--------------------------------------------------end"<<endl; | ||
refresh(); | ||
} | ||
} | ||
|
||
void BFS(Map &aMap, const MNode &startNode, const MNode &endNode, map<int, int>& originMap) | ||
{ | ||
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size() <= 0) | ||
{ | ||
stringstream ss; | ||
ss<<"invalid start node:"<<startNode.ToString()<<" or end node:"<<endNode.ToString()<<endl; | ||
cout<<ss.str(); | ||
endwin(); | ||
return; | ||
} | ||
list<MNode> waveList; | ||
waveList.push_back(startNode); | ||
vector<MNode> reachedVector; | ||
reachedVector.push_back(startNode); | ||
list<MNode> nbList; | ||
while(!waveList.empty()) | ||
{ | ||
MNode ckNode = waveList.front(); | ||
if(!ckNode.IsSamePos(startNode) && !ckNode.IsSamePos(endNode)) | ||
{ | ||
ckNode.NStateSetter(ENodeState::FINDDING); | ||
aMap.Draw(ckNode, EDrawType::STATE); | ||
} | ||
nbList = aMap.GetNeighbors(ckNode, nbList); | ||
|
||
//map.Draw(nbList); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
//refresh(); | ||
for ( MNode nextNode: nbList) | ||
{ | ||
//如果还没有检查过,插入到下一个要找的步骤 | ||
auto checkedFun = [nextNode](MNode node){return node.IsSamePos(nextNode); }; | ||
if(originMap.find(aMap.GetNodeNum(nextNode)) == originMap.cend()) | ||
{ | ||
originMap.insert(make_pair<int, int>(aMap.GetNodeNum(nextNode), aMap.GetNodeNum(ckNode))); | ||
} | ||
if(find_if(reachedVector.cbegin(), reachedVector.cend(), checkedFun) == reachedVector.cend()) | ||
{ | ||
waveList.push_back(nextNode); | ||
if(!nextNode.IsSamePos(startNode) && !nextNode.IsSamePos(nextNode)) | ||
{ | ||
nextNode.NStateSetter(ENodeState::NEXT); | ||
} | ||
reachedVector.push_back(nextNode); | ||
} | ||
} | ||
waveList.pop_front(); | ||
//map.Draw(nbList); | ||
refresh(); | ||
} | ||
} | ||
|
||
void Dijkstra(const Map &map, const MNode &startNode, const MNode &endNode) | ||
{ | ||
|
||
} | ||
|
||
void AStar(const Map &map, const MNode &startNode, const MNode &endNode) | ||
{ | ||
|
||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
#ifndef ASTAR_ALGORITHM_H | ||
#define ASTAR_ALGORITHM_H | ||
#include<queue> | ||
#ifndef _ASTAR_ALGORITHM_H | ||
#define _ASTAR_ALGORITHM_H | ||
#include "algorithm.h" | ||
#include "map.h" | ||
#include "mnode.h" | ||
|
||
void ConstructMap(Map& aMap); | ||
|
||
void BFS(Map& aMap, const MNode& startNode, const MNode& endNode, map<int, int>&); | ||
void Dijkstra(const Map& aMap, const MNode& startNode, const MNode& endNode); | ||
void AStar(const Map& aMap, const MNode& startNode, const MNode& endNode); | ||
|
||
class AStarAlgorithm : public Algorithm | ||
{ | ||
public: | ||
AStarAlgorithm() = default; | ||
AStarAlgorithm(Map& _map):Algorithm(_map){} | ||
AStarAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){} | ||
void Resolve() override; | ||
void FindPath() override; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <algorithm> | ||
#include <chrono> | ||
#include <curses.h> | ||
#include <iostream> | ||
#include <list> | ||
#include <sstream> | ||
#include <thread> | ||
#include <vector> | ||
#include "mnode.h" | ||
#include "debug_tool.h" | ||
#include "astar_algorithm.h" | ||
using std::cout; | ||
using std::endl; | ||
using std::stringstream; | ||
using std::vector; | ||
using std::find_if; | ||
using std::make_pair; | ||
|
||
static map<ENodeType, list<pair<int,int>>> ElementsMap = | ||
{ | ||
{ENodeType::BARRIER, | ||
{ | ||
{13,4},{14,4},{15,4}, {15,4},{16,4}, {17,4},{18,4},{19,4}, | ||
{13,5},{14,5},{15,5}, {15,5},{16,5}, {17,5},{18,5},{19,5}, | ||
{0,4},{1,4},{2,4}, {3,4},{4,4}, {5,4},{6,4},{7,4},{8,4},{9,4},{10,4},{11,4},{11,5},{11,6},{11,7},{11,8},{11,9},{11,10},{11,11}, | ||
{0,5},{1,5},{2,5}, {3,5},{4,5}, {5,5},{6,5},{7,5}, | ||
} | ||
}, | ||
{ENodeType::WATER, | ||
{ | ||
{2,12},{3,12},{4,12},{5,12},{6,12},{7, 12}, {8,12}, {9,12},{10,12},{11,12},{12,12}, | ||
{2,13},{3,13},{4,13},{5,13},{6,13},{7, 13}, {8,13}, {9,13},{10,13},{11,13},{12,13} | ||
} | ||
} | ||
}; | ||
|
||
void ConstructMap(Map &aMap) | ||
{ | ||
if(aMap.Size() > 0) | ||
{ | ||
//cout<<endl; | ||
//cout<<"constsruct--------------------------------------------------start"<<endl; | ||
for (pair<ENodeType, list<pair<int, int>>> ePair : ElementsMap) | ||
{ | ||
for (pair<int, int> miPair : ePair.second) | ||
{ | ||
MNode& node = aMap.GetNode(miPair.first, miPair.second); | ||
node.NTypeSetter(ePair.first); | ||
//cout<<node.NType<<"$->"<<node.str<<","; | ||
aMap.Draw(node, EDrawType::TYPE); | ||
} | ||
} | ||
//cout<<endl; | ||
//cout<<"constsruct--------------------------------------------------end"<<endl; | ||
refresh(); | ||
} | ||
} | ||
|
||
void BFS(Map &aMap, const MNode &startNode, const MNode &endNode, map<int, int>& originMap) | ||
{ | ||
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size() <= 0) | ||
{ | ||
stringstream ss; | ||
ss<<"invalid start node:"<<startNode.ToString()<<" or end node:"<<endNode.ToString()<<endl; | ||
cout<<ss.str(); | ||
endwin(); | ||
return; | ||
} | ||
list<MNode> waveList; | ||
waveList.push_back(startNode); | ||
vector<MNode> reachedVector; | ||
reachedVector.push_back(startNode); | ||
list<MNode> nbList; | ||
while(!waveList.empty()) | ||
{ | ||
MNode ckNode = waveList.front(); | ||
if(!ckNode.IsSamePos(startNode) && !ckNode.IsSamePos(endNode)) | ||
{ | ||
ckNode.NStateSetter(ENodeState::FINDDING); | ||
aMap.Draw(ckNode, EDrawType::STATE); | ||
} | ||
nbList = aMap.GetNeighbors(ckNode, nbList); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
for ( MNode nextNode: nbList) | ||
{ | ||
//如果还没有检查过,插入到下一个要找的步骤 | ||
auto checkedFun = [nextNode](MNode node){return node.IsSamePos(nextNode); }; | ||
if(originMap.find(aMap.GetNodeNum(nextNode)) == originMap.cend()) | ||
{ | ||
originMap.insert(make_pair<int, int>(aMap.GetNodeNum(nextNode), aMap.GetNodeNum(ckNode))); | ||
} | ||
if(find_if(reachedVector.cbegin(), reachedVector.cend(), checkedFun) == reachedVector.cend()) | ||
{ | ||
waveList.push_back(nextNode); | ||
if(!nextNode.IsSamePos(startNode) && !nextNode.IsSamePos(nextNode)) | ||
{ | ||
nextNode.NStateSetter(ENodeState::NEXT); | ||
} | ||
reachedVector.push_back(nextNode); | ||
} | ||
} | ||
waveList.pop_front(); | ||
refresh(); | ||
} | ||
} | ||
|
||
void FindBFSPath(const map<int, int> & originMap, const MNode& startNode, const MNode& endNode, vector<int>& pathVec) | ||
{ | ||
if(originMap.size()>0) | ||
{ | ||
} | ||
} | ||
|
||
void Dijkstra(const Map &map, const MNode &startNode, const MNode &endNode) | ||
{ | ||
|
||
} | ||
|
||
void AStar(const Map &map, const MNode &startNode, const MNode &endNode) | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef ASTAR_ALGORITHM_H | ||
#define ASTAR_ALGORITHM_H | ||
#include<queue> | ||
#include <vector> | ||
#include "map.h" | ||
#include "mnode.h" | ||
using std::vector; | ||
|
||
void ConstructMap(Map& aMap); | ||
|
||
void BFS(Map& aMap, const MNode& startNode, const MNode& endNode, map<int, int>&); | ||
void FindBFSPath(const map<int, int>&, const MNode& startNode, const MNode& endNode, vector<int>&); | ||
void Dijkstra(const Map& aMap, const MNode& startNode, const MNode& endNode); | ||
void AStar(const Map& aMap, const MNode& startNode, const MNode& endNode); | ||
|
||
#endif |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef _BFS_ALGORITHM_H | ||
#define _BFS_ALGORITHM_H | ||
#include "algorithm.h" | ||
#include "map.h" | ||
class BFSAlgorithm : public Algorithm | ||
{ | ||
public: | ||
BFSAlgorithm() = default; | ||
BFSAlgorithm(Map& _map):Algorithm(_map){} | ||
BFSAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){} | ||
void Resolve() override; | ||
void FindPath() override; | ||
}; | ||
#endif |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef _DIJ_ALGORITHM_H | ||
#define _DIJ_ALGORITHM_H | ||
#include "algorithm.h" | ||
#include "map.h" | ||
class DIJAlgorithm : public Algorithm | ||
{ | ||
public: | ||
DIJAlgorithm() = default; | ||
DIJAlgorithm(Map& _map):Algorithm(_map){} | ||
DIJAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){} | ||
void Resolve() override; | ||
void FindPath() override; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#ifndef _MAP_CONFIG_H | ||
#define _MAP_CONFIG_H | ||
|
||
#endif |