迷宫问题求解算法Word文档格式.docx
- 文档编号:17232226
- 上传时间:2022-11-29
- 格式:DOCX
- 页数:19
- 大小:83.75KB
迷宫问题求解算法Word文档格式.docx
《迷宫问题求解算法Word文档格式.docx》由会员分享,可在线阅读,更多相关《迷宫问题求解算法Word文档格式.docx(19页珍藏版)》请在冰豆网上搜索。
~Coordinate(){}
voidSet_row(intr){row=r;
}
voidSet_col(intc){col=c;
intGet_row(){returnrow;
intGet_col(){returncol;
};
classMazenode
introw,col;
intdir,ord;
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_dir(){returndir;
classMaze
char**grid;
Maze(intr,intc,multimap<
int,int>
gg)
row=r+2;
col=c+2;
intrr,cc;
multimap<
:
iteratorpos;
//initthemaze
grid=newchar*[r+2];
for(inti=0;
i<
r+2;
i++)
grid[i]=newchar[c+2];
for(intj=0;
j<
c+2;
j++)
grid[i][j]='
0'
;
//settheobscal
{
grid[0][i]='
1'
grid[r+1][i]='
}
grid[i][0]='
grid[i][c+1]='
for(pos=gg.begin();
pos!
=gg.end();
++pos)
rr=pos->
first;
cc=pos->
second;
grid[rr][cc]='
~Maze()
col;
delete[]grid[i];
delete[]grid;
char**Get_grid(){returngrid;
voidPrintmaze()
cout<
<
"
Themazeinformationisfollow:
endl;
row;
cout<
grid[i][j]<
"
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);
case3:
//left
temp=pos.Get_col()-1;
case4:
//up
temp=pos.Get_row()-1;
default:
it'
swrong!
exit(0);
returnpos;
intPassMaze(Maze*mm,Coordinatestart,Coordinateend)
vector<
Mazenode>
path;
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;
row_c;
map[i]=newint[col_c];
cout<
themapis:
for(intj=0;
col_c;
if(mm->
Get_grid()[i][j]=='
)
map[i][j]=1;
else
map[i][j]=0;
map[i][j]<
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;
}
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());
mm->
delete[]map[i];
delete[]map;
if(path.empty())
return0;
for(pos=path.begin();
=path.end();
("
pos->
Get_row()<
"
Get_col()<
)"
return1;
主函数调用为:
#include"
stdafx.h"
example35_maze.h"
intmain(void)
{
introw,col,rr,cc;
multimap<
way;
creatthemaze:
(x=?
y=?
)"
cin>
>
row>
theobscalecoordinate:
cin>
rr>
cc;
if(rr==0||cc==0)
break;
elseif(rr<
0||cc<
0||rr>
row||cc>
col)
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))
can'
tgoout."
return0;
实验结果展示:
第二次实现的较为完整的算法:
intt1=(*(--path.end())).Get_row(),t2=(*(--path.end())).Get_col();
if(e.Get_row()==t1&
e.Get_col()==t2)
path.pop_back();
if(!
intt1=(*(--path.end())).Get_row(),t2=(*(--path.end())).Get_col();
if(e.Get_row()==t1&
path.pop_back();
thewayisfollow:
delete[]map[i];
delete[]map;
theloadisfollow:
for(pos=path.begin();
return1;
else
0||cc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 迷宫 问题 求解 算法