Data Structure (ZKNU OJ) Maze DFS Method

Description

There is a 10 x 10 maze, the start is 'S', the end is 'E', the walls are '#', and the roads are spaces. A robot goes from the beginning to the end. When the robot walks to a channel block and there is no way to go in front, it will turn to the right-hand direction it is facing and continue to walk. If the robot can pass, leave a footprint '*', if it can't go, leave a mark '!'. The algorithm in the book is given below, please simulate the movement of the robot and output the final state.

Input

A 10 x 10 two-dimensional character array.

Output

The state of the path the robot has traveled.

Sample Input

##########
#S #   # #
#  #   # #
#    ##  #
# ###    #
#   #    #
# #   #  #
# ### ## #
##      E#
##########

Sample Output

##########
#**#!!!# #
# *#!!!# #
#**!!##  #
#*###    #
#***#    #
# #***#  #
# ###*## #
##   ****#
##########

OK, this question is a simplified version of the maze, you don't need to find the shortest path, you don't need to find all the paths, you just need to follow the robot's walking rules to the end point.

First, we have to analyze the establishment of data types.

When the robot is walking, the information we need to know is 1, the current position coordinate 2, and the current direction the robot is facing.

So we create data type Maze

typedef struct Maze{
	int x;
	int y;//coordinates
	int dir; // face direction
}Maze;

Secondly, the input data is a two-dimensional character array. For the convenience of operation, we convert the maze information into a two-dimensional array. We first agree

Direction: Right 1 Down 2 Left 3 Up 4 (Clockwise)

7 for entry 8 for exit 5 for passage block 6 for passage through and back

1 for wall 0 for passable

After doing this, we analyze the problem-solving ideas:

1. Read in the data and convert it into a two-dimensional array.

2. Get the coordinates of the starting point and the ending point ( note that the starting point and the ending point are different each time, so you need to find them ), push the starting point coordinates on the stack, enter the loop, and start walking the maze

3. In each loop, first determine the direction dir the robot is facing, and then determine whether the front can pass

1) If it can pass, set the current coordinate value to 5, then push the next coordinate to go on the stack, and then reset the direction to 1! (Every time you enter a new channel block, you need to face 1 to achieve the effect of finding all directions)!

 2) If you can't pass, turn. dir+=1;

    The code when the direction is 1:

if(seat.dir==1)
		{
			if(a[seat.x][seat.y+1]==0)
			{
				a[seat.x][seat.y+1]=5;
				seat.y+=1;
				seat.dir=1;//Every time you enter a new channel block, you need to face 1
				push(&S,seat);
				continue;
			}
			else
			{
				seat.dir=seat.dir+1;//Steering
				continue;
			}
		}

3) The key point is that when the direction is 4, it means that the first three directions of the channel block are not accessible. If the fourth direction is not accessible at this time, the stack will be popped and the position will be set to 6, indicating that it cannot go through. After popping the stack, set the coordinate direction of the top of the stack to 1, and then continue to use it to loop

Code for direction 4

if(seat.dir==4)
		{
			if(a[seat.x-1][seat.y]==0)
			{
				a[seat.x-1][seat.y]=5;
				seat.x-=1;
				seat.dir=1;
				push(&S,seat);
				continue;
			}
			else
			{
				pop(&S,&out);//退栈
				a[out.x][out.y]=6;//The mark is not working
				seat.x=( (S.top)-1)->x;
				seat.y=( (S.top)-1)->y;
				seat.dir=1;//The coordinate direction of the top of the stack is set to 1
				continue;
			}
		}

 4) After the end of each cycle, judge whether the coordinates are equal to the exit coordinates. If they are equal, it means the maze is over.

4. Convert the two-dimensional array into a two-dimensional character array and print it out. 6 correspondence! 5 corresponds to * 1 corresponds to # 0 corresponds to space

The idea of ​​solving the problem can be compared with the coordinate diagram and go through the cycle process by yourself


The specific code is as follows:

The operation of finding the starting point and the ending point in the code is to traverse the entire array. This can be optimized, and you can change it when you have time.

#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct Maze{
	int x;
	int y;//coordinates
	int dir; // face direction
}Maze;
typedef struct node{
	Maze *base;
	Maze *top;
}Sqtack;
void Input(int a[15][15]);
void Output(int a[15][15]);
void Initstack(Sqtack *L);
int push(Sqtack *S,Maze elem);
int pop(Sqtack *S,Maze *seat);

//Direction right 1 down 2 left 3 up 4 clockwise
//7 represents the entrance 8 represents the exit 5 represents the coordinates passed by 6 represents the coordinates passed and returned
// 1 for wall 0 for passable
intmain()
{
	int a[15][15];
	int Fx, Fy;
	Input(a);
	//find the starting point
	for(int i=0;i<10;i++)
	for(int j=0;j<10;j++)
	if(a[i][j]==7)
	{
		Fx=i;
		Fy=j;
		a[i][j]=0;
	}
	
	Sqtack S;
	Initstack(&S);
	Maze seat,out;
	seat.dir=1;
	seat.x=Fx;
	seat.y=Fy;
	a[seat.x][seat.y]=5;
	push(&S,seat);
	
	//find the end point
	for(int i=0;i<10;i++)
	for(int j=0;j<10;j++)
	if(a[i][j]==8)
	{
		Fx=i;
		Fy=j;
		a[i][j]=0;
	}
	
	while(seat.x!=Fx||seat.y!=Fy)
	{
		if(seat.dir==1)
		{
			if(a[seat.x][seat.y+1]==0)
			{
				a[seat.x][seat.y+1]=5;
				seat.y+=1;
				seat.dir=1;//Every time you enter a new channel block, you need to face 1
				push(&S,seat);
				continue;
			}
			else
			{
				seat.dir=seat.dir+1;//Steering
				continue;
			}
		}
		if(seat.dir==2)
		{
			if(a[seat.x+1][seat.y]==0)
			{
				a[seat.x+1][seat.y]=5;
				seat.x+=1;
				seat.dir=1;
				push(&S,seat);
				continue;
			}
			else
			{
				seat.dir=seat.dir+1;//Steering
				continue;
			}
		}
		if(seat.dir==3)
		{
			if(a[seat.x][seat.y-1]==0)
			{
				a[seat.x][seat.y-1]=5;
				seat.y-=1;
				seat.dir=1;
				push(&S,seat);
				continue;
			}
			else
			{
				seat.dir=seat.dir+1;//Steering
				continue;
			}
		}
		if(seat.dir==4)
		{
			if(a[seat.x-1][seat.y]==0)
			{
				a[seat.x-1][seat.y]=5;
				seat.x-=1;
				seat.dir=1;
				push(&S,seat);
				continue;
			}
			else
			{
				pop(&S,&out);//退栈
				a[out.x][out.y]=6;//The mark is not working
				seat.x=( (S.top)-1)->x;
				seat.y=( (S.top)-1)->y;
				seat.dir=1;//The coordinate direction of the top of the stack is set to 1
				continue;
			}
		}
	}
	Output(a);
}
void Input(int a[15][15])//Read in data
{
	char ch[15];
	for(int i=0;i<10;i++)
	{
		gets(ch);
		int l=strlen(ch);
		for(int j=0;j<10;j++)
		{
			if(ch[j]==' ')
			{
				a[i][j]=0;
			}
			if(ch[j]=='#')
			{
				a[i][j]=1;
			}
			if(ch[j]=='S')
			{
				a[i][j]=7;
			}
			if(ch[j]=='E')
			{
				a[i][j]=8;
			}
		}
	}
}
void Output(int a[15][15])//Print the result
{
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		{
			if(a[i][j]==1)
			{
				printf("#");
			}
			if(a[i][j]==0)
			{
				printf(" ");
			}
			if(a[i][j]==5)
			{
				printf("*");
			}
			if(a[i][j]==6)
			{
				printf("!");
			}
		}
		if(i!=9)
		printf("\n");
	}
}
void Initstack(Sqtack *S)//Initialization
{
	S->base=(Maze *)malloc(500*sizeof(Maze));
	S->top=S->base;
}
int push(Sqtack *S,Maze seat)//Push stack
{
	*(S->top)=seat;
	S->top+=1;
}
int pop(Sqtack *S,Maze *out)//弹栈
{
	if(S->base==S->top)
	{
		return -1;
	}
	else
	{
		S->top-=1;
		*out=*(S->top);
		*out=*(S->top);
	}
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325384685&siteId=291194637