This article describes the C++ implementation maze algorithm in an example form. The maze in this example is a rectangular area with an entrance and an exit. The interior of the maze contains walls or obstacles that cannot be crossed. Obstacles are placed along rows and columns, which are parallel to the rectangular boundary of the maze. The entrance to the maze is in the upper left corner and the exit is in the lower right corner
The main functions of this example maze algorithm are:
1. Automatically generate 10*10 maze diagram
2. Determine whether there is a maze exit and draw a roadmap
The specific implementation code is as follows:
# include <iostream> # include <list> # include <sys/> # include <> # include <> using namespace std; bool Makework(int Sam[10][10]);//Judge whether there is an exit for the mazevoid main() { struct _timeb timebuffer; _ftime(&timebuffer); unsigned short int tem=; 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);//The delay achieves the effect of completely random numbers_ftime(&timebuffer); tem=; srand(tem); a=rand()%2; if(rand()%6==1)//Add a random one again, adding 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, and the maze roadmap is as follows"<<endl; } else { cout<<"There is no exit for the maze"<<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<<"Select 1 to continue, other exit"<<endl; cin>>quit; } } bool Makework(int Sam[10][10]) { int x=0,y=0;//x horizontal y vertical coordinate Sam[y][x]int U=-1,D=1,L=-1,R=1;//Up, down, left and rightlist<int> val; list<int>::iterator vben=(); list<int>::iterator vend=(); bool back=false;//Whether it is backward, the current, backward, left and right cannot move.while((9!=x)||(9!=y))//Whether to reach the end point{ if((y+D)<10)//Move down{ if(Sam[y+D][x]==0) { Sam[y][x]=4; if(back)//There is a new route when retreating{ Sam[y+D][x]=4;//The new route is set as a new starting pointback=false; } val.push_back(x);// Add coordinates into containerval.push_back(y); y=y+D;//Move coordinatescontinue; } } if((x+R)<10)//Move 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)//Move up{ 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 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(!())//There is no way to move forward, backward, left and right, or there is a blockage after moving, so backward.{ back=true; list<int>::iterator vend=(); --vend; y=*vend; --vend; x=*vend;//Modify the coordinatesval.pop_back(); val.pop_back(); continue; } else { return false; } } return true; }