C++實(shí)現(xiàn)迷宮算法實(shí)例解析
本文以實(shí)例形式描述了C++實(shí)現(xiàn)迷宮算法。本例中的迷宮是一個(gè)矩形區(qū)域,它有一個(gè)入口和一個(gè)出口。在迷宮的內(nèi)部包含不能穿越的墻或障礙。障礙物沿著行和列放置,它們與迷宮的矩形邊界平行。迷宮的入口在左上角,出口在右下角
本實(shí)例迷宮算法的功能主要有:
1.自動(dòng)生成10*10迷宮圖
2.判斷是否有迷宮出口,并且畫出路線圖
具體實(shí)現(xiàn)代碼如下:
# include <iostream>
# include <list>
# include <sys/timeb.h>
# include <time.h>
# include <windows.h>
using namespace std;
bool Makework(int Sam[10][10]);//判斷迷宮是否有出口
void main()
{
struct _timeb timebuffer;
_ftime(&timebuffer);
unsigned short int tem=timebuffer.millitm;
unsigned short int a=0;
srand(tem);
int quit=1;
int Mou[10][10];
while(quit==1)
{
for(int i=0;i<10;i++)
{
for(int c=0;c<10;c++)
{
Sleep(3);//延時(shí)達(dá)到完全隨機(jī)數(shù)的效果
_ftime(&timebuffer);
tem=timebuffer.millitm;
srand(tem);
a=rand()%2;
if(rand()%6==1)//再次增加一個(gè)隨機(jī),增加空格。
{
a=0;
}
Mou[i][c]=a;
}
cout<<endl;
}
Mou[0][0]=0;
Mou[9][9]=0;
for(int e=0;e<10;e++)
{
for(int d=0;d<10;d++)
{
if(0==Mou[e][d])
{
cout<<"O"<<" ";
}
else
{
cout<<Mou[e][d]<<" ";
}
}
cout<<endl;
}
cout<<endl;
if(Makework(Mou))
{
cout<<"迷宮有出口,迷宮路線圖如下"<<endl;
}
else
{
cout<<"迷宮無出口"<<endl;
}
for(int o=0;o<10;o++)
{
for(int p=0;p<10;p++)
{
if(4==Mou[o][p])
{
cout<<"*"<<" ";
}
else if(0==Mou[o][p])
{
cout<<"O"<<" ";
}
else
{
cout<<Mou[o][p]<<" ";
}
}
cout<<endl;
}
cout<<"選擇1繼續(xù),其它退出"<<endl;
cin>>quit;
}
}
bool Makework(int Sam[10][10])
{
int x=0,y=0;//x橫y縱坐標(biāo)Sam[y][x]
int U=-1,D=1,L=-1,R=1;//上下左右
list<int> val;
list<int>::iterator vben=val.begin();
list<int>::iterator vend=val.end();
bool back=false;//是否是在后退,當(dāng)前后左右都不能移動(dòng)時(shí)。
while((9!=x)||(9!=y))//是否到達(dá)終點(diǎn)
{
if((y+D)<10)//下移動(dòng)
{
if(Sam[y+D][x]==0)
{
Sam[y][x]=4;
if(back)//后退時(shí)有新的路線
{
Sam[y+D][x]=4;//新路線設(shè)置為新起點(diǎn)
back=false;
}
val.push_back(x);//坐標(biāo)添加進(jìn)容器
val.push_back(y);
y=y+D;//移動(dòng)坐標(biāo)
continue;
}
}
if((x+R)<10)//右移動(dòng)
{
if(Sam[y][x+R]==0)
{
Sam[y][x]=4;
if(back)
{
Sam[y][x+R]=4;
back=false;
}
val.push_back(x);
val.push_back(y);
x=x+R;
continue;
}
}
if(y+U>=0)//上移動(dòng)
{
if(Sam[y+U][x]==0)
{
Sam[y][x]=4;
if(back)
{
Sam[y+U][x]=4;
back=false;
}
val.push_back(x);
val.push_back(y);
y=y+U;
continue;
}
}
if((x+L>=0))//左移動(dòng)
{
if(Sam[y][x+L]==0)
{
Sam[y][x]=4;
if(back)
{
Sam[y][x+L]=4;
back=false;
}
val.push_back(x);
val.push_back(y);
x=x+L;
continue;
}
}
if(!val.empty())//前后左右不能移動(dòng)或者移動(dòng)后都有阻擋,那么后退。
{
back=true;
list<int>::iterator vend=val.end();
--vend;
y=*vend;
--vend;
x=*vend;//修改坐標(biāo)
val.pop_back();
val.pop_back();
continue;
}
else
{
return false;
}
}
return true;
}
相關(guān)文章
c++ 內(nèi)聯(lián)函數(shù)和普通函數(shù)的區(qū)別
內(nèi)聯(lián)函數(shù)是c++為了提高程序的運(yùn)行速度做的改進(jìn),那么內(nèi)聯(lián)函數(shù)和普通函數(shù)的區(qū)別是什么,本文就來詳細(xì)的介紹一下,感興趣的朋友可以了解一下2021-05-05
C++11中bind綁定器和function函數(shù)對(duì)象介紹
這篇文章主要介紹了C++11中bind綁定器和function函數(shù)對(duì)象介紹,綁定器,函數(shù)對(duì)象和lambda表達(dá)式只能使用在一條語(yǔ)句中,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-07-07
C++中的移動(dòng)構(gòu)造函數(shù)及move語(yǔ)句示例詳解
這篇文章主要給大家介紹了關(guān)于C++中移動(dòng)構(gòu)造函數(shù)及move語(yǔ)句的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
深入分析C++中兩個(gè)大數(shù)相乘結(jié)果不正確的問題
本篇文章是對(duì)C++中兩個(gè)大數(shù)相乘結(jié)果不正確的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

