Maze Solver!

Source: Internet
Author: User
Tags printf

problem: Find the path to the exit from the entrance to the maze.

algorithm: In order to describe the layout of the maze, set the definition of the maze array m[][] to global variables to reduce the parameter passing. In addition, a structure is needed to describe the coordinates of the maze footprint, as defined below:

struct Maze_location

{int x; Line coordinates

int y; column coordinates};

The int Cur_num is the number variable of the record channel, and whenever there is a walking channel, the cur_num adds 1 and finally finds a path, the path is presented in 1,2,3,4 .... A line that is sequentially numbered. At first it is necessary to set the coordinates of the entrance and exit, and the block corresponding to the entry coordinates should be (to be exact) a channel block, so the coordinates corresponding to the position are assigned a value of 1, that is, cur_num the current initial number. Set the channel block and wall for the maze (the value of the channel block is-1, the wall is 0) the general flow of the program is as follows:

Follow the directions (from East---north) to find the path

{

If the next step is a channel block, then

                                          {

number plus 1, and assign to the next footstep block;

if the coordinates of this step at this point are the export coordinates

{The path is printed directly; (a path has been found.) )}

otherwise

{The algorithm is called recursively, but the parameters are changed at this point, the parameters are the coordinates of the current step and the current number (each recursive argument is the next)}

number minus 1;

restores the value of the block to-1;

                                          }

}

void Findpath (maze_location curpos,int cur_num)
{int i;
Maze_location Next;
Maze_location direction[4]={{0,1},{-1,0},{0,-1},{1,0}};//Change Direction
for (i=0;i<=3;i++)
{
next.x=curpos.x+direction[i].x;
NEXT.Y=CURPOS.Y+DIRECTION[I].Y;
if (m[next.x][next.y]==-1)//IF channel block
{
M[next.x][next.y]=++cur_num; Ref. +1
if (next.x!=ending.x | | next.y!=ending.y)//The next coordinate will continue to be recursive as long as it is not an export coordinate
Findpath (Next,cur_num);
Else
{
Print ();
printf ("\ n");
}
M[next.x][next.y]=-1;
cur_num--;
}
}
}

The procedure is as follows:

#include "C1.h" #define Maxlenth 5 struct maze_location {int x;
int y;
};
typedef int MAZE[MAXLENTH][MAXLENTH];  Maze m;   Create Maze m, defined as global variable reduce parameter maze_location start,ending;//entry coordinate, export coordinate int cur_num=1; Initialize ordinal//Sub function start void print () {int i,j; for (i=0;i<maxlenth;i++) {for (j=0;j<maxlenth;j++) printf ("%3d", M[i][j]
	);
printf ("\ n");
    }} void Initmaze (int k) {int i,n,j,x1,y1; for (i=0;i<maxlenth;i++) m[0][i]=m[i][0]=m[maxlenth-1][i]=m[i][maxlenth-1]=0;//around a value of 0, 0 for the wall printf ("Please enter the maze you want to decorate in addition to
    The number of walls: \ n ");
    scanf ("%d", &n); for (i=1;i<maxlenth-1;i++) {for (j=1;j<maxlenth-1;j++) m[i][j]=k;//The interior of the maze is all 1, i.e. all channels} printf ("Now start the wall for the maze, follow
The coordinates of the wall within the second input maze: (all columns starting from 0) \ n ");
	for (j=1;j<=n;j++)//cloth n Wall {scanf ("%d,%d", &x1,&y1);
m[x1][y1]=0;
} print ();//Printing Maze printf ("Please enter entry coordinates: \ n");
scanf ("%d,%d", &AMP;START.X,&AMP;START.Y);
printf ("Please enter export coordinates: \ n");
scanf ("%d,%d", &ending.x,&ending.y);
	} void Findpath (Maze_location curpos,int cur_num) {int i; Maze_location Next Maze_location direction[4]={{0,1},{-1,0},{0,-1},{1,0}};//Note East is the column plus 1 rows unchanged, and the corresponding in the array is y plus 1 for (i=0;i<=3;i++) {Next.x
		=curpos.x+direction[i].x;
		NEXT.Y=CURPOS.Y+DIRECTION[I].Y;
		    if (m[next.x][next.y]==-1) {m[next.x][next.y]=++cur_num;
			  if (next.x!=ending.x | | next.y!=ending.y) findpath (next,cur_num);//continue to search for else {print () as long as the exit is not reached;
			printf ("\ n");           } m[next.x][next.y]=-1;//Two sentences is very important, if the block is a channel block but there is no other way out, that is, only the original way back, then cur_num--;
	To cancel the number previously assigned to this block, revert to 1}} void Main () {Initmaze (-1);
	M[start.x][start.y]=1;
	printf ("The path to the maze is as follows: \ n");
Findpath (Start,cur_num); }


This algorithm is very well understood, but a little bit difficult to understand, I think for a long time, that is, the following situation:

0 0 0 0

-1-1 0 0

0 0 0 0

assuming that the red part of the current footsteps, and now start to go east, we have a big eye to know that actually can not go east, because the next step, although the channel block, but it is a dead end, there is no way to find the next path, it can only return the original path. But how does this manifest in the program? This I think for a long time, is the last two sentences above:

M[next.x][next.y]=-1;

cur_num--;

when the footstep has the first red-1 run away to the next 1, it is first discovered that it is a channel block, which is numbered (assuming the ordinal 2), as follows:

0 0 0 0

1 2 0 0

0 0 0 0

Found at this time of this-1 (ordinal 2) channel in addition to the original direction there is no way to go, then it will return, but if you return directly then the next time will come here, so you get into the cycle of death, so to cancel its number, let it revert to the normal channel block, the serial number also minus 1 (to assign value to other There is no need to worry about meeting this dead end, because at this time has reached the beginning of the program, the Red 1 in the footsteps of the next for loop, that is, change direction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.