Maze algorithm--to verify the continuity of the maze

Source: Internet
Author: User

"QBoy Original"

Remember to study data structures in the university to learn to use an iterative approach to determine whether a maze has a solution or even to find out all the possible exits. At present, the company developed a game needs players to build a maze to let other players to the maze of the game, the game is actually very simple. But one key point is to verify that the player's maze is reachable. Well, that's what we're going to do with the algorithm. Write down this article just for the mark process.

First create the necessary structure

(1) All node types in the Maze

typedef enum{
empty=0,//Blank
obstacle,//disorders
enter,//Entrance
exit,//exit
Types of}mazenodetype;//Maze nodes

Depending on the business requirements, other node types may also be involved, which is the most basic type.

(2) Maze node:

typedef struct
{
Ccpoint location;//Location
Mazenodetype NodeType;
}mazenode;

(3) Maze map

typedef struct
{
mazenode** nodes of the pmazenode;//map
int imazeorientation;//Number of horizontal
int imazelongitudinal;//Portrait

mazenode* penter;//Entrance

}mazemap;

(4) Defining direction enumeration when walking a maze

typedef enum{
up = 0,//
left,//Left
down,//under
right,//Right
Maxdirection
} mazedirection;//Direction

For simplicity, a static array is defined that describes the value of each x, y change in each direction.

  Static Ccpoint POINTDIRECTION[]={CCP (0,1), CCP ( -1,0), CCP (0,-1), CCP (1,0)};

Second Verification maze

BOOL Checkmazeroad (mazemap& map, Mazenodetype SelectType)
{
Ccassert (Map.pmazenode!=null, "map is NULL");
Ccassert (map.imazeorientation>0, "Orientation must larger than 0");
Ccassert (map.imazelongitudinal>0, "longitudinal must larger than 0");
Std::stack<mazenode*> smazenode;//non-validated nodes
Std::list<mazenode*> lmazenode;//Validated nodes
Smazenode.push (map.penter);//inlet into the stack
Lmazenode.push_back (Map.penter);
BOOL Rtn = false;
Do
{
mazenode* Pnode = Smazenode.top ();
Smazenode.pop ();

for (int i = 0;i<maxdirection&&!rtn;i++)//Four directions in turn
{
int x = (int) (pnode->location.x+pointdirection[i].x);
int y = (int) (PNODE->LOCATION.Y+POINTDIRECTION[I].Y);

if (x < 0 | | x >= map.imazelongitudinal | | y < 0 | | y >= map.imazeorientation)
{
continue;//out of bounds to remove a
}

mazenode* NewNode = &map.pMazeNode[x][y];
Std::list<mazenode*>::iterator it = Find (Lmazenode.begin (), Lmazenode.end (), NewNode);
if (It!=lmazenode.end ())//find indicates that the point has been judged or is still not processed in the stack
{

Continue

}else{
if (newnode->nodetype== selecttype)//Export
{
RTN = true;
}else if (newnode->nodetype==empty) {//blank input stack facilitates next check
Lmazenode.push_back (NewNode);
Smazenode.push (NewNode);
}
}
}

} while (Smazenode.size () >0&&!rtn);//No exits or exits have been found
return RTN;
}

Since this function is just to satisfy whether there is an exit route to be determined, if an exit route is required, a pointer to the front node is added to the Maze node and the maze path is found in reverse.

This function is relatively simple, so directly on the code, forgive me.

Maze algorithm--to verify the continuity of the 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.