Skip to content
Permalink
Browse files

first commit

  • Loading branch information
chenyukang committed Mar 27, 2012
0 parents commit f236f7008b116958200445e2df76ae0cb90952b6
Showing with 53,239 additions and 0 deletions.
  1. +78 −0 A_star.cpp
  2. +36 −0 A_star.h
  3. BIN A_star.o
  4. +149 −0 Algorithm.cpp
  5. +88 −0 Algorithm.h
  6. BIN Algorithm.o
  7. +65 −0 DFS_L.cpp
  8. +23 −0 DFS_L.h
  9. BIN DFS_L.o
  10. +65 −0 DFS_R.cpp
  11. +23 −0 DFS_R.h
  12. BIN DFS_R.o
  13. +33 −0 DisjSets.cpp
  14. +21 −0 DisjSets.h
  15. BIN DisjSets.o
  16. +404 −0 Maze.cpp
  17. +73 −0 Maze.h
  18. BIN Maze.o
  19. +51,956 −0 cscope.out
  20. +211 −0 main.cpp
  21. BIN main.o
  22. +14 −0 makefile
@@ -0,0 +1,78 @@
#include "A_star.h"
#include "Maze.h"
#include <math.h>

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)
{
Di[0][0]=-1;Di[0][1]=0;
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_pMaze->SetCellVisited(x,y,true);
int v=this->EvalFunc(x,y);
PQ.push(Node(x,y,v));
cout<<"A_star constructed"<<endl;
}


int A_star::EvalFunc(int x,int y)
{
int xdiff=abs(x-m_pMaze->GetHeight()-1);
int ydiff=abs(y-m_pMaze->GetWidth()-1);
return xdiff+ydiff;
// if(xdiff>ydiff)
// return ydiff;
// else
// return xdiff;
}

bool A_star::search()
{
if(!PQ.empty())
{
Node now=PQ.top();
PQ.pop();
int now_id=m_pMaze->Getid(now.x,now.y);
for(int i=0;i<4;i++)
{
int nx=now.x+Di[i][0];
int ny=now.y+Di[i][1];
if(m_pMaze->IsValid(nx,ny)&&
m_pMaze->IsValidNextPath(now.x,now.y,nx,ny))
{
int v=this->EvalFunc(nx,ny);
PQ.push(Node(nx,ny,v));
m_pathmap[m_pMaze->Getid(nx,ny)]=now_id;
m_pMaze->SetCellVisited(nx,ny,true);
}
}
personx=now.x;
persony=now.y;
}
return false;
}

const vector<int>& A_star::GetPath()//return the path from source to target
{
if(m_path.size()==0)
{
cout<<"constructor result path"<<endl;
int target=m_pMaze->Getid(m_pMaze->GetHeight()-1,m_pMaze->GetWidth()-1); // get the target id
int id_now=m_pathmap[target];
int from=m_pMaze->Getid(fromx,fromy);
while(id_now!=from)
{
m_path.insert(m_path.begin(),id_now);
id_now=m_pathmap[id_now];
}
}
return m_path;

}
@@ -0,0 +1,36 @@
#ifndef A_STAR_H_
#define A_STAR_H_
#include "Algorithm.h"
#include <queue>
using namespace std;

class A_star:public AlgorithmBase
{
class Node
{
public:
int x,y;
int wei;
friend bool operator > (const Node& lhs,const Node& rhs)
{
return lhs.wei>rhs.wei;
}
Node(int xx,int yy,int w):x(xx),y(yy),wei(w)
{
}
};
private:
bool search();
int Di[4][2];
map<int,int> m_pathmap;
priority_queue<Node,vector<Node>,greater<Node> > PQ;//优先队列
int EvalFunc(int x,int y);

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

#endif//A_STAR_H_
BIN +247 KB A_star.o
Binary file not shown.
@@ -0,0 +1,149 @@
#include "Algorithm.h"
#include "Maze.h"
#include <iostream>
#include <iomanip>
using namespace std;


AlgorithmBase::AlgorithmBase(Maze* maze,int x,int y):m_pMaze(maze),personx(x),persony(y)
{
fromx=x;
fromy=y;
}


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

}

const vector<int>& AlgorithmBase::GetPath()
{
return m_path;
}

DFS::DFS(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;
Di[3][0]=1;Di[3][1]=0;
m_stack.push(Node(x,y));
cout<<"DFS constructed"<<endl;
}

bool DFS::search()
{
if(!m_stack.empty())
{
Node step=m_stack.top();
m_stack.pop();
m_pMaze->SetCellVisited(step.x,step.y,true);//set this node visited
for(int i=0;i<4;i++)
{
int nx=step.x+Di[i][0];
int ny=step.y+Di[i][1];
if(m_pMaze->IsValid(nx,ny)&&
m_pMaze->IsValidNextPath(step.x,step.y,nx,ny))
{
Node node(nx,ny);
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;
}


const vector<int>& DFS::GetPath()
{
if(m_path.size()==0)
{
cout<<"constructor result path"<<endl;
int target=m_pMaze->Getid(m_pMaze->GetHeight()-1,m_pMaze->GetWidth()-1); // get the target id
int id_now=m_pathmap[target];
int from=m_pMaze->Getid(fromx,fromy);
while(id_now!=from)
{
m_path.insert(m_path.begin(),id_now);
id_now=m_pathmap[id_now];
}
}
return m_path;
}

void DFS::Draw()
{
return;
}



BFS::BFS(Maze* maze,int x,int y):AlgorithmBase(maze,x,y)
{
m_queue.push(Node(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;
Di[3][0]=1;Di[3][1]=0;
m_pMaze->SetCellVisited(x,y,true);
cout<<"BFS constructed"<<endl;
}

bool BFS::search()
{
if(!m_queue.empty())
{
Node step=m_queue.front();
m_queue.pop();
int id_now=m_pMaze->Getid(step.x,step.y);

for(int i=0;i<4;i++)
{
int nx=step.x+Di[i][0];
int ny=step.y+Di[i][1];
if(m_pMaze->IsValid(nx,ny)&&
m_pMaze->IsValidNextPath(step.x,step.y,nx,ny))
{
Node node(nx,ny);
m_queue.push(node);
m_pathmap[m_pMaze->Getid(nx,ny)]=m_pMaze->Getid(step.x,step.y);
m_pMaze->SetCellVisited(nx,ny,true);
}
}

personx=step.x;
persony=step.y;
}
return false;
}

const vector<int>& BFS::GetPath()//return the path from source to target
{
if(m_path.size()==0)
{
cout<<"constructor result path"<<endl;
int target=m_pMaze->Getid(m_pMaze->GetHeight()-1,m_pMaze->GetWidth()-1); // get the target id
int id_now=m_pathmap[target];
int from=m_pMaze->Getid(fromx,fromy);
while(id_now!=from)
{
m_path.insert(m_path.begin(),id_now);
id_now=m_pathmap[id_now];
}
}
return m_path;

}

void BFS::Draw()
{
return ;
}
@@ -0,0 +1,88 @@
#ifndef _ALGORITHM_H__
#define _ALGORITHM_H__

#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
using namespace std;

class Maze;

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

class AlgorithmBase
{
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;

public:
AlgorithmBase(Maze*,int,int);
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();

};

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();
};


#endif
BIN +274 KB Algorithm.o
Binary file not shown.

0 comments on commit f236f70

Please sign in to comment.
You can’t perform that action at this time.