I believe everyone should have heard of the stack, has always wanted to use the stack to implement an algorithm, recently a little empty, the use of the advanced features of the stack to complete the maze of the problem, the following will be introduced in detail the implementation of the stack and maze solution process, can be very good to learn the use of the stack.
There are two implementations of the stack, one is the order, that is, the array form, one is linear, that is, the form of the chain table, the individual or more like the basic function of the chain list to implement the stack.
First get a simple maze,
We can easily see the path of the maze, how to let the computer help us solve it.
First of all, we need to data the maze, it is easy to think of two-dimensional array to represent, 0 means the path, 1 represents a barrier, which is expressed as:
int map[4][4]={ {0,1,1,0}, {0,0,0,0}, {0,0,1,1}, {1,0,0,0} };
Our idea is to start from the entrance, explore the direction of exploration, if able to walk, then continue to move forward, otherwise, along the original road back, another direction to explore, until the test exit. See this idea and find out how appropriate it is to use stacks. Let's implement the Maze solution.
I. Implementation of the Stack
We will first implement the stack of stacks, such as the stack of basic stack construction
Define the structure of the stack:
struct node{ int x;//row int y;//column corresponds to map[x][y ] int DIRECTION;//1 represents right, 2 is down, 3 is left, 4 is up, store exploration direction, Next explore the direction of node *next; int above;//in which direction is explored down, to prevent the left and right up and down dead Loop node () { next=null; }};
Defines the head pointer of the stack: node *head=null;
Data into the Stack function:
The stack---inserted into the head void Push (node * &head,int x,int y,int direction,int above) { node * temp=new node; temp->x=x;temp->y=y;temp->direction=direction; temp->above=above; if (head==null) head=temp; else { temp->next=head; head=temp;} }
Data out-of-stack implementations:
Out of the stack--take head after void Pop (node * &head) { node * OK; if (head!=null) { ok=head; head=head->next; Delete OK;} }
The basic function of the stack is realized. So we can begin to solve the maze. Truth-seeking, understanding the principle, code writing is not difficult.
The Maze Solver steps are as follows:
First put the entry into the stack: Push (head,0,0,1,0);//ingress into the stack
Explore the code specifically://Explore the path while (true) {if (Getpop (head). X==3&&getpop (head). y==3)//Find exit, stop loop break; Probing the next piece of bool sea=true; Node temp; Each step path explores while (SEA) {Temp=getpop (head); The temp.above!=4 condition prevents the right or the upper and lower loops to go//temp.direction for the forward Direction switch (temp.direction) {CA SE 1://Exploration success, that is, right can, and the direction of the previous step is not left, to prevent death cycle, exploration success, direction plus one, easy to go back to another direction to explore if ((temp.y+1) <blocknum& ; &map[temp.x][temp.y+1]==0&&temp.above!=3) {change (head,temp.direction+1); Push (head,temp.x,temp.y+1,1,1);//explore successfully into the stack sea=false; Stop Loop} else {change (head,temp.direction+1) ; Explore the right to failure, explore the direction plus one, continue to explore} break; Case 2:if (temp.x+1) <blocknum&&map[temp.x+1][temp.y]==0&&temp.above!=4) {change (head,temp.direction+1); Push (head,temp.x+1,temp.y,1,2); Sea=false; } else {change (head,temp.direction+1); } break; Case 3:if ((temp.y-1) >=0&&map[temp.x][temp.y-1]==0&&temp.above!=1) { Change (head,temp.direction+1); Push (head,temp.x,temp.y-1,1,3); Sea=false; } else {change (head,temp.direction+1); } break; Case 4:if ((temp.x-1) >=0&&map[temp.x-1][temp.y]==0&&temp.above!=2) { Change (head,temp.direction+1); Push (head,temp.x-1,temp.y,1,4); Sea=false; } else {Pop (head); Sea=false; } break; default://that when the temp.direction==5, this block has been all directions have been explored, put him out of the stack Pop (head); Sea=false; Break } } }
The maze solution can be achieved by the above two while loops.
Stack for Maze solver (C + +)