An example of maze algorithm based on C language
- 2020-05-30 20:46:51
- OfStack
This paper illustrates the maze algorithm based on C. I will share it with you for your reference as follows:
Maze algorithm using c language, environment is vc++6.0.
#include<stdio.h>
#include<time.h>
#include<cstdlib>
int visit(int,int);
void setmaze();
int maze[11][11]=
{
{0,0,2,2,2,2,2,2,2,2},
{2,0,2,2,0,2,0,2,0,2},
{2,0,2,0,0,0,0,0,0,2},
{2,0,2,2,2,0,2,0,0,2},
{2,0,0,0,0,0,2,2,0,2},
{2,2,0,2,2,0,2,2,0,2},
{2,2,2,0,0,0,0,0,0,2},
{2,0,2,0,2,0,2,2,0,2},
{2,0,0,0,0,2,0,2,0,0},
{2,2,2,2,2,2,2,2,2,2}
};
int startI,startJ; // Define entry variables
int endI,endJ; // Define exit variables
int success=0; // Define the return variable
int p;
void setStart() // Set the entrance
{
printf(" Please set the maze entrance ( i,j ) :");
scanf("%d,%d",&startI,&startJ);
}
void setEnd() // Set the export
{
printf(" Please set the maze exit ( i,j ) :");
scanf("%d,%d",&endI,&endJ);
}
void setmaze() // Set the maze diagram
{
int i,j,a,p;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
p=rand()%2;
if(p==0) a=0;
else a=2;
maze[i][j]=a;
}
}
void DisplayMaze() // Print a maze
{
int i,j;
for(i=0;i<10;i++)
{
printf(" ");
for(j=0;j<10;j++)
if(maze[i][j]==2) printf("##"); // Print the wall
else printf(" "); // Print the path
printf("/n");
}
}
void Maze_PS() // Output maze path
{
int i,j;
if(visit(startI,startJ)==0) // Search path
printf("/n No way out! /n");
else
{
maze[startI][startJ]=8;// Set up entrance marks
maze[endI][endJ]=9;// Set up exit marks
printf("/n Display path: /n");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(maze[i][j]==8) printf(" Do things ");// Marks the entrance
else if(maze[i][j]==9) printf(" a. ");// Mark export
else if(maze[i][j]==2) printf("##"); // Said the wall
else if(maze[i][j]==1) printf(" >"); // Said path
else printf(" "); // pathways
}
printf("/n");
}
}
}
int visit(int i,int j) // Find the maze path function, find the path back 1 , could not find the path back 0
{
maze[i][j]=1;
if((i==endI)&&(j==endJ)) success=1; // Find the exit and return the value success for 1
if((success!=1)&&(maze[i][j+1]==0)) visit(i,j+1); // Detect the right channel and move to the right if it is open
if((success!=1)&&(maze[i+1][j]==0)) visit(i+1,j); // Detect the following path, if it is open, move down
if((success!=1)&&(maze[i][j-1]==0)) visit(i,j-1); // Detect the left side of the path. If it is open, move to the left
if((success!=1)&&(maze[i-1][j]==0)) visit(i-1,j); // Detect the upper channel and move upward if it is open
if(success!=1) maze[i][j]=0; // Return and label itself as 0
return success;
}
main(void) // The main function
{
int c1,c2;
for(c2=1;c2==1;)
{
srand(time(0));
printf(" Show the maze: /n");
for(c1=1;c1==1;)
{
DisplayMaze();
printf(" According to the '1' Output new maze ,'2' Start solving path: /n");
scanf("%d",&c1);
rewind(stdin);// Clear input buffer
if(c1==1) setmaze() ;
}
if(c1!=1&&c1!=2) {printf("Error!!/n");break;}
setStart();
setEnd();
Maze_PS();
printf("Continue?(1 to continue,2 to exit.1)");
scanf("%d",&c2);
if(c2==1) setmaze();
else break;
}
system("pause");
}
I hope this article has been helpful to you in the programming of C language.