Recently started to learn the data structure, found that the data structure is really a magical thing ha, a lot of real problems can be used in different data knot
To solve, such as using and stack infix expression to write a computer program, the use of stacks to crack maze game, today I will share with you
Enjoy how to use stacks to crack maze games.
People who have learned the data structure know that the characteristics of the stack are: LIFO (first in the last out), which means that only the tail of the stack
The stack and the stack are stacked, and the stack can only start with the last data. Such as:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/D8/wKioL1cLCx6TXT38AAAfpXDCVH8769.png "title=" Image.png "alt=" Wkiol1clcx6txt38aaafpxdcvh8769.png "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
And we are in the maze game to use the method is "backtracking", that is, in the search for access, each find a path, the data will be stacked, so that the position of the previous position is located on the top of the stack, if the current position up and down can not find access to the time, it began to backtrack, That is the beginning of the road back to walk, and the road before the existence of the stack inside, so only need a pop can go back in turn, each back, looking up and down there is no access, if find access to continue down, and pressure stack, until out of the entire maze. That's probably the idea.
First of all, how to build a maze, if manually input in the program, it is cumbersome and inflexible, so we can first write the maze into a file, and then in the program to read the file, so OK spicy. Such as
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/D8/wKioL1cLC2Ph9nFtAAAe_sf_Upo180.png "title=" Image.png "alt=" Wkiol1clc2ph9nftaaae_sf_upo180.png "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
And then we're going to read the maze in the file, the specific code:
void Getmaze (int *a, int n) {file* fout = fopen ("Maze Map.txt", "R"), Assert (Fout), for (int i = 0; i < n;i++) {for (int J = 0; J < N;) {char ch = fgetc (fout); if (ch = = ' 0 ' | | ch = = ' 1 ') {a[i*n + j] = ch-' 0 '; ++j; Do not convert}else{continue when reading a space;}} Fclose (fout);}
After reading the maze will begin to look for the same side, starting from the entrance to determine whether the left or right is empty, in order to look more intuitive, we
To write a function to check if there is a path, and then go down, the specific code is as follows:
Bool checkaccess (Int *a, int n, pos& next) {if (next.row>0 | | next.row<n | | next.col>0 | | next.col < n | | //guarantee the validity of each location a[next.row*n + next.col] == 0) {return true;} Else{return false;}} Bool mazepath (int* a, int n, pos& entry,stack<pos> path) {pos cur = entry;path.push (cur);while (!path.empty ()) {if (cur.row == n - 1) {return true;} a[cur.row*n + cur.col] = 2; //saved the previous position pos next = cur; next.row--; //Road if (CheckAccess (A,n,next)) {Next=cur;path.push (cur); continue;} next.row++; //Xia Lu if (CheckAccess (A, n, next)) {Next=cur;path.push (cur); continue;} next.col--; // Left if (CheckAccess (A, n, next)) {Next= cur;path.push (cur); continue;} next.col++; // On the if (CheckAccess (A, n, next)) {Next = cur;path.push (cur); continue;} cur = path.top (); path.pop (); }return false;}
Below we can print out the maze:
void Printmaze (int *a, int n) {for (int i = 0; i < n; i++) {for (int j = 0; J < N; j + +) {cout << a[i*n + j] <& Lt " ";} cout << Endl;} cout << Endl;}
The above is the stack cracked maze method, in fact, this code is not good, there are many places need to improve and optimize, such as how to find the shortest path, etc., I hope readers can actively for me to point out, learn from each other, common progress.
This article from "Fu da Xin" blog, reproduced please contact the author!
Data structure application: Using stacks to crack maze game