Stacks are an important linear structure in data structures, which limits the linear table of inserts and deletions only at the end of the table, so we can also think of it as a special linear table. Because of this feature of the stack, we can call it a last-in-first-out structure. :
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/DF/wKioL1cLi_vRu4ebAAARqXsqTA8989.png "title=" capture. PNG "alt=" Wkiol1cli_vru4ebaaarqxsqta8989.png "/>
because the stack has a last-in-first-out nature, we can use it as a useful tool in programming. Using the stack we can implement the number conversion, suffix expression evaluation, maze solver and so on. In the book we can see the simple idea of C language implementation, but there are still many bugs in the program. Today, I would like to try to use a powerful C + + to achieve.
the solution of the maze problem is generally from the entrance, along a direction to explore, if you can walk, then continue to explore, if not to walk, then another direction to explore, until all the possible channels are explored. Using the characteristics of the stack, we can explore the path of the road into the stack, if encountered the road is not through the stack operation, fallback, repeat exploration.
PS: For the sake of convenience I used a notepad to store the maze, with 1 means no, 0 means the pathway. Mark the journey through 2.
Code implementation:
struct pos //can be used to access the location by subscript {int _row;int _col;}; Void getmaze (int *a,int n)//read out the Maze map {file* fout=fopen ("Maze.txt", "R") from the file, 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;} else{ continue;}}} Fclose (fout);} Bool checkisaccess (int *a,int n,pos next)//Check for access {assert (a); if ( (next._row>=0) && (next._row<n) && (next._col>=0) && (next._col <n) && (a[next._row*n+next._col]==0) {return true;} Else{return false;}} Bool mazepath (Int *a,int n,pos& entry,stack<pos>& path) //exploration Process {Pos cur=entry;path.push (cur); while (!path.empty ()) {pos next=cur;a[cur._row*n+cur._col]=2 ; if (next._row==n-1) /*| | next._row==0 | | next._col==n-1 | | &nBsp;next._col==0*/{return true;} Determine if the upper and lower left or right is access next=cur;next._ROW--;// on if (checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;} next=cur;next._row++;//under if (Checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;} next=cur;next._col--;//left if (checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;} next=cur;next._col++;//Right if (checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;} Fallback cur=path.top ();p ath.pop ();} }void printmaze (int *a,int n) //output Maze {for (int i=0; i<n; i++) {for (int j=0; j<n; j++) { cout<<a[i*n+j]< < " ";} Cout<<endl;}}
This article is from the "July boreas" blog, please be sure to keep this source http://luminous.blog.51cto.com/10797288/1762736
Application stack solving maze problem (C + + implementation)