Using the SDK to realize the maze algorithm

Source: Internet
Author: User

I recently looked at the data structure of the book, now the textbook or use the written algorithm of C/s + +, compiled or in console mode, if you can use these data structure algorithms in the SDK, then you can develop the algorithm of Windows programs to improve learning, not in The monotonous console mode looks at the cold characters to learn the data structure, so that learning can learn to invoke Windows API and Windows programming, on the other hand can learn data structure. I hope my learning method is helpful to the novice Windows friends.

This is the Maze program developed using the SDK (F1 key begins).

Maze algorithm or the old way, backtracking and stack implementation, I am using the stack implementation. Use a two-way linked list to touch the stack, using a ptrfirst and a ptrlast and a stack-bottom and stack-top pointer to define a stack-element structure that holds the position in the maze.

typedef struct _tagNode {
  int  nRow;
  int  nColumn;
  struct _tagNode* next;
  struct _tagNode* previou;
} Node;
Defines a tag array

BOOL bPass[ Row ][ Column ];  // Row 和 Column 为迷宫大小.The method of realizing the main pseudocode of Maze algorithm.

A. Start from the beginning, judge all directions of the ball is feasible, if one direction is feasible, then move in that direction.

Forward position into the stack.

Condition: The forward direction is a wall, then the direction cannot forward.

Forward and direction if it is passed, the direction cannot be forward.

if (Canmove (Gnrow, Gncolumn, right)
{
Gncolumn + = MoveRight;/forward
bpass[Gnrow] [ Gncolumn] = TRUE;    Mark through the Position
//Gnrow, gncolumn position into the stack.

Else if (Canmove (Gnrow, Gncolumn, left))//The right side does not pass, to the right.
{
Gncolumn + = moveLeft/forward
bpass[Gnrow] [gncolumn] = TRUE;//Mark through location
//Gnrow, G Ncolumn position into the stack.   
}
Else if (Canmove (Gnrow, Gncolumn, Forward))//left side does not pass, forward.
{
Gncolumn + = Moveforward/forward
bpass[Gnrow] [gncolumn] = TRUE;//Mark through location
//Gnrow, Gncolu MN position into stack.   
}
Else if (Canmove (Gnrow, Gncolumn, back))//forward not pass, backward.
{
Gncolumn + = Moveback/forward
bpass[Gnrow] [gncolumn] = TRUE;//Mark through location
//Gnrow, Gncolumn Position into the stack.
}
B. All directions are not feasible, return to the previous position, using the back stack operation, back to a.

else if ( CanMove( gnRow, gnColumn, back ) )
   {}
A, B keep repeating until you find the exit, or traverse the maze (stack empty)

if 为迷宫出口
     bSearch = FALSE;
   if 栈为空 // 没有出口,
     bSearch = FALSE;

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.