Stack to implement a small maze

Source: Internet
Author: User

Summary: The algorithm to realize the maze mainly lies in finding and backtracking. Every location we look for after the entrance starts to determine if the path to the other three directions (not including the path you just walked through) can be reached, and if the general rule goes to the next position, and the previous position is labeled. Continue at this position as the current position. If the other three directions in a position fail, you need to backtrack back to where you can go. We need to label the paths we have traveled so that we can go back faster.

650) this.width=650; "title=" Qq20160411204826.png "alt=" wkiol1clopezjvi8aaaldgwdss8957.png "src="/HTTP/ S2.51cto.com/wyfs02/m02/7e/e0/wkiol1clopezjvi8aaaldgwdss8957.png "/>

First we go along the Orange route starting from the starting position, mark the path we have traveled to 2, and eventually we will go to a dead end, and follow the purple path to know the same way.

Let's take a look at implementing the code

BOOL Mazepath (int* a,int n,const pos& entry,stack<pos>& path) {Pos cur=entry;path.push (cur); while (! Path.empty ()) {a[cur._row*n+cur._col]=2;if (cur._row==n-1) {return true;}    else{//on Pos next=cur;    next._row--; if (checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;} Right next=cur;next._col++;if (checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;} Under Next=cur;next._row++;if (Checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;}       Left Next=cur;next._col--;if (checkisaccess (A,n,next)) {Cur=next;path.push (cur); continue;} Cur=path.top ();p ath.pop ();}}

This program is implemented by pressing the stack, and out of the stack. First we come to a simple understanding of the stack, the stack is only from a port to pop and push, it is because of this feature of the stack, we can walk through the maze of the path into the stack, when entering a dead end can be retrospective only need to out of the stack can.


Bo Master First write, write bad place hope everyone to forgive 650) this.width=650; "alt=" C_0016.gif "src=" http://img.baidu.com/hi/babycat/C_0016.gif "/ >

This article is from the "11440755" blog, please be sure to keep this source http://luodn.blog.51cto.com/11440755/1763872

Stack to implement a small maze

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.