Algorithm: Mouse Walking Maze problem (beginning)
"Write in front."
The recursive realization of the mouse maze problem is an application of recursive thought.
"Problem description"
Given a two-dimensional array, 2 of the array represents the wall, 0 represents the path, and the array can be shown as a maze diagram. Given the inlet and outlet positions, determine if there is a pathway between them and show the way out of the maze.
Code
Description part of the topic
intmigo[7][7]={{2,2,2,2,2,2,2},{2,0,0,0,0,0,2},{2,0,2,0,2,0,2},{2,0,0,0,0,2,2},{2,2,0,2,0,2,2},{2,0,0,0,0,0,2},{2,2,2,2,2,2,2}};//Maze Diagramintstartx=1, starty=1;
int endx=5, endy=5;
| Description:
1. Give an array to describe the maze information.
2. Give the entry and exit coordinates.
Recursive implementation part
intflag=0;intFindintXinty) {Migo[x][y]=1; if(x==endx&&y==EndY) Flag=1; if(migo[x][y+1]==0&&flag!=1) find (x, y+1); if(migo[x][y-1]==0&&flag!=1) find (x, y-1); if(migo[x+1][y]==0&&flag!=1) Find (x+1, y); if(migo[x-1][y]==0&&flag!=1) Find (x-1, y); if(flag!=1) Migo[x][y]=0; returnFlag;}
| Description:
1. What is the meaning of the first sentence of code migo[x][y]=1? We set it to 1 at the beginning, which means that we begin to move around with this axis, and then pivot to the next point ... No paragraph to judge, direct to us find access, namely flag=1. But what we need to understand is that once we can't find a pathway along a path, the last line of code
Then restore it to 0, after exploring all the paths to the maze, we may find the pathway, and every element of that road will be given 1, if none, then it will not.
2. On the idea of recursion: constantly with a certain point as the axis, spread everywhere, in the find exit point will stop recursion.
Road Show Implementation Section
intMainintargcChar**argv) { inti,j; printf ("Show Maze: \ n"); for(i=0;i<7; i++) { for(j=0;j<7; j + +) if(migo[i][j]==2) printf ("█"); Elseprintf (" "); printf ("\ n"); } if(Find (startx,starty) = =0) {printf ("We didn't find the exit! \ n"); } Else{printf ("\ n Display path: \ n"); for(i=0;i<7; i++) { for(j=0;j<7; j + +) { if(migo[i][j]==2) printf ("█"); Else if(migo[i][j]==1) printf ("*"); Elseprintf (" "); } printf ("\ n"); } } return 0;}
| Description:
Slightly.
Algorithm: Mouse Maze problem