迷宫问题求解算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
思想:采用试探法,从某一点出发,先向右探测,如果不同;就向下探测;还是不通就向左探测亦然不通就返回前一个结点。
第一次源代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAX 100
class Coordinate
{
private:
int row;
int col;
public:
Coordinate(int r,int c)
{
row=r;
col=c;
}
~Coordinate(){}
void Set_row(int r){row=r;}
void Set_col(int c){col=c;}
int Get_row(){return row;}
int Get_col(){return col;}
};
class Mazenode
{
private:
int row,col;
int dir,ord;
public:
Mazenode()
{
row=-1;
col=-1;
ord=-1;
dir=-1;
}
void Set_ord(int o){ ord=o;}
void Set_row(int r){ row=r;}
void Set_col(int c){ col=c;}
void Set_dir(int d){ dir=d;}
int Get_ord(){return ord;}
int Get_row(){return row;}
int Get_col(){return col;}
int Get_dir(){return dir;}
};
class Maze
{
private:
int row;
int col;
char **grid;
public:
Maze(int r,int c,multimap
{
row=r+2;
col=c+2;
int rr,cc;
multimap
//init the maze
grid=new char *[r+2];
for(int i=0;i grid[i]=new char [c+2]; for(int i=0;i for(int j=0;j grid[i][j]='0'; //set the obscal for(int i=0;i { grid[0][i]='1'; grid[r+1][i]='1'; } for(int i=0;i { grid[i][0]='1'; grid[i][c+1]='1'; } for(pos=gg.begin();pos != gg.end();++pos) { rr=pos->first; cc=pos->second; grid[rr][cc]='1'; } } ~Maze() { for(int i=0;i delete[] grid[i]; delete[] grid; } int Get_row(){return row;} int Get_col(){return col;} char **Get_grid(){ return grid;} void Printmaze() { cout<<"The maze information is follow:"< for(int i=0;i { for(int j=0;j cout< cout< } } }; //get the next coordinate Coordinate Nextcoord(Coordinate pos,int k) { int temp=-1; switch(k) { case 1: //right temp=pos.Get_col()+1; pos.Set_col(temp); break; case 2: //down temp=pos.Get_row()+1; pos.Set_row(temp); break; case 3: //left temp=pos.Get_col()-1; pos.Set_col(temp); break; case 4: //up temp=pos.Get_row()-1; pos.Set_row(temp); break; default: cout<<"it's wrong!"< exit(0); } return pos; } int PassMaze(Maze *mm,Coordinate start,Coordinate end) { vector vector int curstep=1,row_c,col_c,tt; Mazenode e; row_c=start.Get_row(); col_c=start.Get_col(); Coordinate temp(row_c,col_c); //mm->Printmaze(); int **map; row_c=mm->Get_row(); col_c=mm->Get_col(); map=new int *[row_c]; for(int i=0;i map[i]=new int [col_c]; cout<<"the map is:"< for(int i=0;i { for(int j=0;j { if(mm->Get_grid()[i][j] == '1') map[i][j]=1; else map[i][j]=0; cout<