Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
YukangChen committed May 10, 2016
2 parents 72d2cdd + e130fb9 commit 1c84982
Show file tree
Hide file tree
Showing 21 changed files with 95,297 additions and 146 deletions.
6 changes: 5 additions & 1 deletion .gitignore
@@ -1 +1,5 @@
*.o
*.o
*.exe
*.o.d
.ninja_deps
*.a
44 changes: 44 additions & 0 deletions .ninja_log
@@ -0,0 +1,44 @@
# ninja log v5
1 755 0 CMakeFiles/maze.dir/Algorithm.cpp.o 3610f247e2a89264
0 1311 0 Algorithm.o f02d9836aa684578
1 372 0 CMakeFiles/maze.dir/Maze.cpp.o 1b2a8b2d293f4c24
1 565 0 obj/DisjSets.o f4bef513a8a36b2a
1 360 0 CMakeFiles/maze.dir/A_star.cpp.o f8dcb876e2c88284
0 722 0 obj/Algorithm.o 5fef5b771d135e37
2 752 0 DisjSets.o 47e3336f0743c9e1
1 892 0 obj/A_star.o aec7c9dae59f9632
1 722 0 CMakeFiles/maze.dir/DFS_L.cpp.o 26c79e6bb385be75
2 1278 0 Maze.o 21811a9d6602c4f2
1 866 0 obj/DFS_L.o cb45386bcc5b2ef9
0 62 0 build.ninja 90fc65c1669b26eb
807 1497 0 obj/main.exe 617af850dcfb2210
795 1066 0 CMakeFiles/Maze.exe.dir/main.cpp.o 9903e8a1a3e1bb19
1311 1332 0 liball.a 16bb5e95d1489a20
2 871 0 obj/Maze.o fb62b64b2467359f
403 478 0 Maze.exe 48f07671ecb13c3a
3 556 0 CMakeFiles/maze.dir/DisjSets.cpp.o 9747980818295212
722 807 0 obj/liball.a d109a66475468c66
1 861 0 obj/DFS_R.o 41ca1c3369766373
1 711 0 CMakeFiles/maze.dir/DFS_R.cpp.o cabf687068f2e137
0 1186 0 DFS_L.o 886fadf236d05664
0 526 0 main.exe 6b2823bdaefd0c64
0 1217 0 DFS_R.o 5564c6baf4f5e652
372 403 0 libmaze.a da892c5201d372a
0 1279 0 A_star.o f5d1eae60f506e5
1 891 0 obj/DisjSets.o f4bef513a8a36b2a
1 1126 0 obj/DFS_R.o 41ca1c3369766373
1 1126 0 obj/DFS_L.o cb45386bcc5b2ef9
2 1148 0 obj/Maze.o fb62b64b2467359f
0 1148 0 obj/A_star.o aec7c9dae59f9632
0 1185 0 obj/Algorithm.o 5fef5b771d135e37
1185 1307 0 obj/liball.a d109a66475468c66
1307 2015 0 obj/main.exe 617af850dcfb2210
0 9 0 clean a48d12a494ad532f
1 639 0 obj/DisjSets.o f4bef513a8a36b2a
0 971 0 obj/DFS_L.o cb45386bcc5b2ef9
0 992 0 obj/DFS_R.o 41ca1c3369766373
2 1013 0 obj/Maze.o fb62b64b2467359f
0 1014 0 obj/A_star.o aec7c9dae59f9632
0 1083 0 obj/Algorithm.o 5fef5b771d135e37
1083 1140 0 obj/liball.a d109a66475468c66
1141 1859 0 obj/main.exe 617af850dcfb2210
8 changes: 3 additions & 5 deletions A_star.cpp
Expand Up @@ -2,15 +2,13 @@
#include "Maze.h"
#include <math.h>

int abs ( int v )
{
int abs (int v) {
if( v >= 0 )
return v;
else return -v;
}

A_star::A_star(Maze* maze,int x,int y):AlgorithmBase(maze,x,y)
{
A_star::A_star(Maze* maze,int x,int y):AlgorithmBase(maze,x,y) {
Di[0][0]=-1;Di[0][1]=0;
Di[1][0]=0;Di[1][1]=-1;
Di[2][0]=0;Di[2][1]=1;
Expand Down Expand Up @@ -74,5 +72,5 @@ const vector<int>& A_star::GetPath()//return the path from source to target
}
}
return m_path;

}

1 change: 1 addition & 0 deletions A_star.h
Expand Up @@ -4,6 +4,7 @@
#include <queue>
using namespace std;


class A_star:public AlgorithmBase
{
class Node
Expand Down
19 changes: 10 additions & 9 deletions Algorithm.cpp
@@ -1,4 +1,3 @@
#include "Algorithm.h"
#include "Maze.h"
#include <iostream>
#include <iomanip>
Expand All @@ -11,11 +10,13 @@ AlgorithmBase::AlgorithmBase(Maze* maze,int x,int y):m_pMaze(maze),personx(x),pe
fromy=y;
}

AlgorithmBase::~AlgorithmBase() {
}

void AlgorithmBase::MovePerson()
{
search();
m_pMaze->SetPerson(personx,persony);
search();
m_pMaze->SetPerson(personx,persony);

}

Expand All @@ -30,7 +31,7 @@ DFS::DFS(Maze* maze,int x,int y):AlgorithmBase(maze,x,y)
Di[1][0]=0;Di[1][1]=-1;
Di[2][0]=0;Di[2][1]=1;
Di[3][0]=1;Di[3][1]=0;
m_stack.push(Node(x,y));
m_stack.push(Node(x,y));
cout<<"DFS constructed"<<endl;
}

Expand All @@ -52,11 +53,11 @@ bool DFS::search()
m_stack.push(node);
m_pathmap[m_pMaze->Getid(nx,ny)]=m_pMaze->Getid(step.x,step.y);
}
}
}

personx=step.x;
persony=step.y;

}
return false;
}
Expand Down Expand Up @@ -118,7 +119,7 @@ bool BFS::search()
m_pMaze->SetCellVisited(nx,ny,true);
}
}

personx=step.x;
persony=step.y;
}
Expand All @@ -140,7 +141,7 @@ const vector<int>& BFS::GetPath()//return the path from source to target
}
}
return m_path;

}

void BFS::Draw()
Expand Down
113 changes: 57 additions & 56 deletions Algorithm.h
Expand Up @@ -11,78 +11,79 @@ using namespace std;
class Maze;

enum AlgorithmType{
none,
Dfs,
Bfs,
Left,//靠左走 直到没有路径 右转
Right,//靠右走 直到没有路径 左转
Astar//A* 算法
none,
Dfs,
Bfs,
Left,//靠左走 直到没有路径 右转
Right,//靠右走 直到没有路径 左转
Astar//A* 算法
};

class AlgorithmBase
{
protected:
class Node
{
public:
int x;
int y;
protected:
class Node
{
public:
int x;
int y;

Node(int x,int y):x(x),y(y)
{
}
bool operator==(const Node& node) const
{
return (node.x==x&&node.y==y);
}
};

Maze* m_pMaze;
vector<int> m_path;
int personx,persony;
int fromx;
int fromy;
Node(int x,int y):x(x),y(y)
{}

public:
AlgorithmBase(Maze*,int,int);
virtual bool search()=0;
virtual void Draw()=0;
void MovePerson();
const virtual vector<int>& GetPath();
bool operator==(const Node& node) const
{
return (node.x==x&&node.y==y);
}
};

Maze* m_pMaze;
vector<int> m_path;
int personx,persony;
int fromx;
int fromy;

public:
AlgorithmBase(Maze*, int, int);
virtual ~AlgorithmBase();
virtual bool search()=0;
virtual void Draw()=0;
void MovePerson();
const virtual vector<int>& GetPath();
};


class DFS:public AlgorithmBase
{
private:
stack<Node> m_stack;
bool search();
void Draw();
void ProcessPath(int id);
map<int,int> m_pathmap;
int Di[4][2];
public:
DFS(Maze*,int,int);
~DFS() {}
const virtual vector<int>& GetPath();
private:
stack<Node> m_stack;
bool search();
void Draw();
void ProcessPath(int id);
map<int,int> m_pathmap;
int Di[4][2];
public:
DFS(Maze*,int,int);
~DFS() {}
const virtual vector<int>& GetPath();

};

class BFS:public AlgorithmBase
{
private:
queue<Node> m_queue;
bool search();
void Draw();
void ProcessPath(int id);
map<int,int> m_pathmap;
int Di[4][2];
public:
BFS(Maze*,int,int);
~BFS() {}
const virtual vector<int>& GetPath();
private:
queue<Node> m_queue;
bool search();
void Draw();
void ProcessPath(int id);
map<int,int> m_pathmap;
int Di[4][2];

public:
BFS(Maze*,int,int);
~BFS() {}
const virtual vector<int>& GetPath();
};


#endif
10 changes: 4 additions & 6 deletions DFS_L.cpp
Expand Up @@ -7,7 +7,7 @@ DFS_L::DFS_L(Maze* maze,int x,int y):AlgorithmBase(maze,x,y)
Di[1][0]=1;Di[1][1]=0;
Di[2][0]=0;Di[2][1]=-1;
Di[3][0]=-1;Di[3][1]=0;
m_stack.push(Node(x,y));
m_stack.push(Node(x,y));
dir_now=0;
cout<<"DFS_L constructed"<<endl;
}
Expand Down Expand Up @@ -37,19 +37,19 @@ bool DFS_L::search()
while(1){
nx=personx+Di[dir_now][0];
ny=persony+Di[dir_now][1];

if(m_pMaze->IsValid(nx,ny)&&
m_pMaze->IsValidNextPath(personx,persony,nx,ny))
{
personx=nx;
persony=ny;
m_path.push_back(m_pMaze->Getid(nx,ny));

return true;
}
dir_now=(dir_now+1)%4;
}

}

const vector<int>& DFS_L::GetPath()
Expand All @@ -61,5 +61,3 @@ void DFS_L::Draw()
{
return;
}


10 changes: 4 additions & 6 deletions DFS_R.cpp
Expand Up @@ -7,7 +7,7 @@ DFS_R::DFS_R(Maze* maze,int x,int y):AlgorithmBase(maze,x,y)
Di[1][0]=1;Di[1][1]=0;
Di[2][0]=0;Di[2][1]=-1;
Di[3][0]=-1;Di[3][1]=0;
m_stack.push(Node(x,y));
m_stack.push(Node(x,y));
dir_now=0;
cout<<"DFS_R constructed"<<endl;
}
Expand Down Expand Up @@ -37,19 +37,19 @@ bool DFS_R::search()
while(1){
nx=personx+Di[dir_now][0];
ny=persony+Di[dir_now][1];

if(m_pMaze->IsValid(nx,ny)&&
m_pMaze->IsValidNextPath(personx,persony,nx,ny))
{
personx=nx;
persony=ny;
m_path.push_back(m_pMaze->Getid(nx,ny));

return true;
}
dir_now=Turn_AntiClock(dir_now);
}

}

const vector<int>& DFS_R::GetPath()
Expand All @@ -61,5 +61,3 @@ void DFS_R::Draw()
{
return;
}


3 changes: 1 addition & 2 deletions DisjSets.cpp
Expand Up @@ -12,7 +12,7 @@ Disjsets::Disjsets(int numElements):s(numElements)

void Disjsets::unionSets(int root1,int root2)
{

if( s[root2] < s[root1] )//root2 is deeper
s[root1] = root2; //make root2 new root
else
Expand All @@ -30,4 +30,3 @@ int Disjsets::find(int x) const
else
return find( s[x] );// find the x's root, return it
}

0 comments on commit 1c84982

Please sign in to comment.