迷宫问题求解算法.docx

上传人:b****3 文档编号:4310118 上传时间:2022-11-29 格式:DOCX 页数:19 大小:83.75KB
下载 相关 举报
迷宫问题求解算法.docx_第1页
第1页 / 共19页
迷宫问题求解算法.docx_第2页
第2页 / 共19页
迷宫问题求解算法.docx_第3页
第3页 / 共19页
迷宫问题求解算法.docx_第4页
第4页 / 共19页
迷宫问题求解算法.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

迷宫问题求解算法.docx

《迷宫问题求解算法.docx》由会员分享,可在线阅读,更多相关《迷宫问题求解算法.docx(19页珍藏版)》请在冰豆网上搜索。

迷宫问题求解算法.docx

迷宫问题求解算法

思想:

采用试探法,从某一点出发,先向右探测,如果不同;就向下探测;还是不通就向左探测亦然不通就返回前一个结点。

第一次源代码如下:

#include

#include

#include

#include

#include

#include

#include

#include

usingnamespacestd;

#defineMAX100

classCoordinate

{

private:

introw;

intcol;

public:

Coordinate(intr,intc)

{

row=r;

col=c;

}

~Coordinate(){}

voidSet_row(intr){row=r;}

voidSet_col(intc){col=c;}

intGet_row(){returnrow;}

intGet_col(){returncol;}

};

 

classMazenode

{

private:

introw,col;

intdir,ord;

public:

Mazenode()

{

row=-1;

col=-1;

ord=-1;

dir=-1;

}

voidSet_ord(into){ord=o;}

voidSet_row(intr){row=r;}

voidSet_col(intc){col=c;}

voidSet_dir(intd){dir=d;}

intGet_ord(){returnord;}

intGet_row(){returnrow;}

intGet_col(){returncol;}

intGet_dir(){returndir;}

};

classMaze

{

private:

introw;

intcol;

char**grid;

public:

Maze(intr,intc,multimapgg)

{

row=r+2;

col=c+2;

intrr,cc;

multimap:

:

iteratorpos;

//initthemaze

grid=newchar*[r+2];

for(inti=0;i

grid[i]=newchar[c+2];

for(inti=0;i

for(intj=0;j

grid[i][j]='0';

//settheobscal

for(inti=0;i

{

grid[0][i]='1';

grid[r+1][i]='1';

}

for(inti=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(inti=0;i

delete[]grid[i];

delete[]grid;

}

intGet_row(){returnrow;}

intGet_col(){returncol;}

char**Get_grid(){returngrid;}

voidPrintmaze()

{

cout<<"Themazeinformationisfollow:

"<

for(inti=0;i

{

for(intj=0;j

cout<

cout<

}

}

};

//getthenextcoordinate

CoordinateNextcoord(Coordinatepos,intk)

{

inttemp=-1;

switch(k)

{

case1:

//right

temp=pos.Get_col()+1;

pos.Set_col(temp);

break;

case2:

//down

temp=pos.Get_row()+1;

pos.Set_row(temp);

break;

case3:

//left

temp=pos.Get_col()-1;

pos.Set_col(temp);

break;

case4:

//up

temp=pos.Get_row()-1;

pos.Set_row(temp);

break;

default:

cout<<"it'swrong!

"<

exit(0);

}

returnpos;

}

intPassMaze(Maze*mm,Coordinatestart,Coordinateend)

{

vectorpath;

vector:

:

iteratorpos;

intcurstep=1,row_c,col_c,tt;

Mazenodee;

row_c=start.Get_row();

col_c=start.Get_col();

Coordinatetemp(row_c,col_c);

//mm->Printmaze();

int**map;

row_c=mm->Get_row();

col_c=mm->Get_col();

map=newint*[row_c];

for(inti=0;i

map[i]=newint[col_c];

cout<<"themapis:

"<

for(inti=0;i

{

for(intj=0;j

{

if(mm->Get_grid()[i][j]=='1')

map[i][j]=1;

else

map[i][j]=0;

cout<

}

cout<

}

do

{

row_c=temp.Get_row();

col_c=temp.Get_col();

if(map[row_c][col_c]==0)

{

map[row_c][col_c]=2;

e.Set_ord(curstep);

e.Set_row(row_c);

e.Set_col(col_c);

e.Set_dir

(1);

path.push_back(e);

if(temp.Get_row()==end.Get_row()&&temp.Get_col()==end.Get_col())

{

break;

}

else

{

temp=Nextcoord(temp,1);

curstep++;

}

}

else

{

if(!

path.empty())

{

pos=--path.end();

e=*pos;

intk1=e.Get_row(),k2=e.Get_col();

while(e.Get_dir()==4&&!

path.empty())

{

map[row_c][col_c]=2;

while(e.Get_row()==k1&&e.Get_col()==k2)

{

path.pop_back();

pos=--path.end();

e=*pos;

}

}

if(e.Get_dir()<4)

{

tt=e.Get_dir()+1;

e.Set_dir(tt);

path.push_back(e);

temp.Set_row(e.Get_row());

temp.Set_col(e.Get_col());

temp=Nextcoord(temp,e.Get_dir());

}

}

}

}while(!

path.empty());

for(inti=0;iGet_row();i++)

delete[]map[i];

delete[]map;

if(path.empty())

return0;

for(pos=path.begin();pos!

=path.end();++pos)

cout<<"("<Get_row()<<","<Get_col()<<")";

cout<

return1;

}

主函数调用为:

#include"stdafx.h"

#include"example35_maze.h"

intmain(void)

{

introw,col,rr,cc;

multimapway;

cout<<"creatthemaze:

(x=?

y=?

)"<

cin>>row>>col;

cout<<"theobscalecoordinate:

"<

do

{

cin>>rr>>cc;

if(rr==0||cc==0)

break;

elseif(rr<0||cc<0||rr>row||cc>col)

{

cout<<"thecoordinateiswrong!

"<

continue;

}

way.insert(make_pair(rr,cc));

}while(rr!

=0);

Mazemm(row,col,way);

Coordinatestart(1,1),end(row,col);

if(!

PassMaze(&mm,start,end))

cout<<"can'tgoout."<

return0;

}

实验结果展示:

第二次实现的较为完整的算法:

#include

#include

#include

#include

#include

#include

#include

#include

usingnamespacestd;

#defineMAX100

classCoordinate

{

private:

introw;

intcol;

public:

Coordinate(intr,intc)

{

row=r;

col=c;

}

~Coordinate(){}

voidSet_row(intr){row=r;}

voidSet_col(intc){col=c;}

intGet_row(){returnrow;}

intGet_col(){returncol;}

};

 

classMazenode

{

private:

introw,col;

intdir,ord;

public:

Mazenode()

{

row=-1;

col=-1;

ord=-1;

dir=-1;

}

voidSet_ord(into){ord=o;}

voidSet_row(intr){row=r;}

voidSet_col(intc){col=c;}

voidSet_dir(intd){dir=d;}

intGet_ord(){returnord;}

intGet_row(){returnrow;}

intGet_col(){returncol;}

intGet_dir(){returndir;}

};

classMaze

{

private:

introw;

intcol;

char**grid;

public:

Maze(intr,intc,multimapgg)

{

row=r+2;

col=c+2;

intrr,cc;

multimap:

:

iteratorpos;

//initthemaze

grid=newchar*[r+2];

for(inti=0;i

grid[i]=newchar[c+2];

for(inti=0;i

for(intj=0;j

grid[i][j]='0';

//settheobscal

for(inti=0;i

{

grid[0][i]='1';

grid[r+1][i]='1';

}

for(inti=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(inti=0;i

delete[]grid[i];

delete[]grid;

}

intGet_row(){returnrow;}

intGet_col(){returncol;}

char**Get_grid(){returngrid;}

voidPrintmaze()

{

cout<<"Themazeinformationisfollow:

"<

for(inti=0;i

{

for(intj=0;j

cout<

cout<

}

}

};

//getthenextcoordinate

CoordinateNextcoord(Coordinatepos,intk)

{

inttemp=-1;

switch(k)

{

case1:

//right

temp=pos.Get_col()+1;

pos.Set_col(temp);

break;

case2:

//down

temp=pos.Get_row()+1;

pos.Set_row(temp);

break;

case3:

//left

temp=pos.Get_col()-1;

pos.Set_col(temp);

break;

case4:

//up

temp=pos.Get_row()-1;

pos.Set_row(temp);

break;

default:

cout<<"it'swrong!

"<

exit(0);

}

returnpos;

}

intPassMaze(Maze*mm,Coordinatestart,Coordinateend)

{

vectorpath;

vector:

:

iteratorpos;

intcurstep=1,row_c,col_c,tt;

Mazenodee;

row_c=start.Get_row();

col_c=start.Get_col();

Coordinatetemp(row_c,col_c);

//mm->Printmaze();

int**map;

row_c=mm->Get_row();

col_c=mm->Get_col();

map=newint*[row_c];

for(inti=0;i

map[i]=newint[col_c];

cout<<"themapis:

"<

for(inti=0;i

{

for(intj=0;j

{

if(mm->Get_grid()[i][j]=='1')

map[i][j]=1;

else

map[i][j]=0;

cout<

}

cout<

}

do

{

row_c=temp.Get_row();

col_c=temp.Get_col();

if(map[row_c][col_c]==0)

{

map[row_c][col_c]=2;

e.Set_ord(curstep);

e.Set_row(row_c);

e.Set_col(col_c);

e.Set_dir

(1);

if(!

path.empty())

{

intt1=(*(--path.end())).Get_row(),t2=(*(--path.end())).Get_col();

if(e.Get_row()==t1&&e.Get_col()==t2)

path.pop_back();

}

path.push_back(e);

if(temp.Get_row()==end.Get_row()&&temp.Get_col()==end.Get_col())

{

break;

}

else

{

temp=Nextcoord(temp,1);

curstep++;

}

}

else

{

if(!

path.empty())

{

pos=--path.end();

e=*pos;

intk1=e.Get_row(),k2=e.Get_col();

while(e.Get_dir()==4&&!

path.empty())

{

map[row_c][col_c]=2;

while(e.Get_row()==k1&&e.Get_col()==k2)

{

path.pop_back();

pos=--path.end();

e=*pos;

}

}

if(e.Get_dir()<4)

{

tt=e.Get_dir()+1;

e.Set_dir(tt);

if(!

path.empty())

{

intt1=(*(--path.end())).Get_row(),t2=(*(--path.end())).Get_col();

if(e.Get_row()==t1&&e.Get_col()==t2)

path.pop_back();

}

path.push_back(e);

temp.Set_row(e.Get_row());

temp.Set_col(e.Get_col());

temp=Nextcoord(temp,e.Get_dir());

}

}

}

}while(!

path.empty());

if(!

path.empty())

{

cout<<"thewayisfollow:

"<

for(inti=0;iGet_row();i++)

{

for(intj=0;jGet_col();j++)

cout<

cout<

}

for(inti=0;iGet_row();i++)

delete[]map[i];

delete[]map;

cout<<"theloadisfollow:

"<

for(pos=path.begin();pos!

=path.end();++pos)

cout<<"("<Get_row()<<","<Get_col()<<")";

cout<

return1;

}

else

{

for(inti=0;iGet_row();i++)

delete[]map[i];

delete[]map;

return0;

}

}

主函数调用为:

#include"stdafx.h"

#include"example35_maze.h"

intmain(void)

{

introw,col,rr,cc;

multimapway;

cout<<"creatthemaze:

(x=?

y=?

)"<

cin>>row>>col;

cout<<"theobscalecoordinate:

"<

do

{

cin>>rr>>cc;

if(rr==0||cc==0)

break;

elseif(rr<0||cc

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 自我管理与提升

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1