C++ implementation of maze algorithm instance analysis

  • 2020-04-02 02:29:32
  • OfStack

This paper describes the maze algorithm implemented by C++ in the form of an example. The maze in this example is a rectangular area with an entrance and an exit. The interior of the maze contains impenetrable walls or obstacles. Obstacles are placed along rows and columns parallel to the rectangular boundaries of the maze. The entrance to the maze is in the upper left corner and the exit is in the lower right corner

The functions of this example maze algorithm mainly include:

1. Generate 10*10 maze map automatically

2. Determine if there is a maze exit and draw a line diagram

The specific implementation code is as follows:


# include <iostream>
# include <list>
# include <sys/timeb.h>
# include <time.h>
# include <windows.h>
using namespace std;
bool Makework(int Sam[10][10]);//Determine if the maze has an exit
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);//Delay to achieve the effect of completely random Numbers
_ftime(&timebuffer);
tem=timebuffer.millitm;
srand(tem);
a=rand()%2;
if(rand()%6==1)//Add another random, add space.
{
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<<" The maze has an exit. The maze route is as follows "<<endl;
}
else
{
cout<<" Maze without exit "<<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<<" choose 1 Continue, others exit "<<endl;
cin>>quit;
}
}
bool Makework(int Sam[10][10])
{
int x=0,y=0;//Sam[y][x]
int U=-1,D=1,L=-1,R=1;//The up and down or so
list<int> val;
list<int>::iterator vben=val.begin();
list<int>::iterator vend=val.end();
bool back=false;//Whether in the back, the front after left and right can not move.
while((9!=x)||(9!=y))//Whether to reach the end
{
if((y+D)<10)//The mobile
{
if(Sam[y+D][x]==0)
{
Sam[y][x]=4;
if(back)//There is a new route in retreat
{
Sam[y+D][x]=4;//The new route is set as the new starting point
back=false;
}
val.push_back(x);//Coordinates are added to the container
val.push_back(y);
y=y+D;//Moving coordinates
continue;
}
}
if((x+R)<10)//The right
{
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)//On the mobile
{
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))//Move the left
{
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())//If you can't move left, right or left, or if you can't move left or right, back off.
{
back=true;
list<int>::iterator vend=val.end();
--vend;
y=*vend;
--vend;
x=*vend;//Modify the coordinates
val.pop_back();
val.pop_back();
continue;
}
else
{
return false;
}
}
return true;
}

Related articles: