雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++實(shí)現(xiàn)迷宮算法實(shí)例解析

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

本文以實(shí)例形式描述了C++實(shí)現(xiàn)迷宮算法。本例中的迷宮是一個(gè)矩形區(qū)域,它有一個(gè)入口和一個(gè)出口。在迷宮的內(nèi)部包含不能穿越的墻或障礙。障礙物沿著行和列放置,它們與迷宮的矩形邊界平行。迷宮的入口在左上角,出口在右下角

本實(shí)例迷宮算法的功能主要有:

1.自動(dòng)生成10*10迷宮圖

2.判斷是否有迷宮出口,并且畫(huà)出路線圖

具體實(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<<"迷宮無(wú)出口"<<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;
}

上一篇:C++實(shí)現(xiàn)獲取IP、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS等本機(jī)網(wǎng)絡(luò)參數(shù)的方法

欄    目:C語(yǔ)言

下一篇:C++日志記錄類實(shí)例解析

本文標(biāo)題:C++實(shí)現(xiàn)迷宮算法實(shí)例解析

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3560.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有