Use stack and queue to implement the maze path Search Algorithm

Source: Internet
Author: User

Use stack and queue to implement the maze path Search Algorithm
0 Summary

Recently, I received a small task from my boss, that is, to find a small program using a maze path, requiring stack and queue. I did not dare to neglect it. I opened Visual Studio 2012 and drew a picture on the paper. The questions are as follows:Design an algorithm for finding the maze path using the stack and queue respectively (use the following two-dimensional array to represent the maze, 1 to indicate that the maze cannot pass, 0 to indicate that the maze can pass, and 2 to the upper left corner as the starting point, (3 in the lower right corner is the end point ).


1. stack implementation

The key point of the Maze path search is the processing of the pivot point. The method for storing the coordinate of the pivot point using Stacks is calledDeep SearchAlgorithm. For the initial matrix, search for the paths in four directions from the initial point 2 and mark the paths properly. Then, you can output the paths to be searched based on the tags. Therefore, the correct labeling scheme in the search process determines the efficiency and feasibility of path search. The stack can adopt the following tag policy:

Based on the value in the question, 2 is the start point, and 3 is the end point, so that flag = 4 is the next reasonable path node weight from 4.

Rule 1: for the current node, if only the exit of the unique path exists, mark the exit node weight as the current node weight, 4 (the start point weight is 4), and 5 (7.

Rule 2: press the pivot point into the stack, and Add 1 to the original right value for each exit point. For example, three exits of four nodes are pushed into the stack, and five are the first egress, 6 is the second output stack.

Mark the weights according to the above rules. You can search for the Paths Based on the top, bottom, and left paths from the start point.Point with the largest weightAs the next node in the path, you can find the final path (2-4-6-7-3 ).


2. Queue implementation

The Implementation Method of Using queues to store pivot coordinate is called the breadth search algorithm. Unlike the stack search method, the tag policy is different. For queues, the following tag policy can be used:

Based on the value in the question, 2 is the start point, 3 is the end point, consistent with the above stack, marking the path from 4.

Rule 1: uniform weights are used for the same level of extended search nodes.

Rule 2: record the number of points at the same level. When the queue enters the next level, add 1 to the weight.

Mark the weights according to the preceding rules, when searching for a path, you can use the reverse search method to search for the minimum value before the end point as the path's valid node (3-9-8-7-6-5-4-2 ).


3. Implementation Code and Result demonstration

/* ===================================================== = ========================================================== = * 20141203 */# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using std: cout; using std: endl; using std: stack; using std: queue; # define N 15 typedef struct {int x; int y;} PointPos; int map [15] [15] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1}, {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }}; /* ===================================================== =============================== * function declaration * ======================== ========================================================== * /// the void FindMarkedPos (PointPos & start, pointPos & end); // The void InitMap () function for restoring the initial map data; // The void UseStack (PointPos start, PointPos end) algorithm based on the Stack depth-first search algorithm ); // The void UseQueue (PointPos start, PointPos end) algorithm based on the Queue-based breadth-first search algorithm; // The void StackOutPut (PointPos start) function of the Stack result output function ); // Queue result output function void QueueOutPut (PointPos start ); /* ===================================================== =============================== * main function * ======================== ========================================================== */void main (void) {// In fact, the end mark point defines PointPos start and end; // The FindMarkedPos (start, end) function for searching the mark point function; // The UseStack (start, end) function for searching the Stack depth first ); // output the Result cout of the Stack deep Priority Search <"Stack-Search Result:" <
      
        Pointstack; for (int I = 0; I <15; I ++) {for (int j = 0; j <15; j ++) {if (2 = map [I] [j]) {start. x = I; start. y = j; flag = flag + 1;} else if (map [I] [j] = 3) {end. x = I; end. y = j; flag = flag + 1;} if (2 = flag) break;} if (2 = flag) break ;}} /* ===================================================== =============================== * Stack-based deep Priority Search Algorithm * ================ ========================================================== ===== */void UseStack (PointPos start, pointPos end) {// defines the storage stack
       
         Pointstack; // mark the path weight, and define the point coordinate and temporary variable int flag = 4; PointPos pointpos, pointtemp; // import the starting point coordinate into the stack and enter the cyclic search pointstack. push (start); while (! Pointstack. empty () {pointpos = pointstack. top (); pointstack. pop (); // count records the number of directions in which each coordinate point has a path: int I, j, count = 0; I = pointpos. x; j = pointpos. y; // cout <
        
          0 & I
         
           0 & j
          
            3) map [I] [j] = 0 ;}} /* ===================================================== ================================= * Based on the Queue-based breadth-first search algorithm * ================ ========================================================== ===== */void UseQueue (PointPos start, pointPos end) {// defines that the weights of the storage queue are the same as those of the sub-pivot, that is, the queue uses the inverse search path algorithm at the same level as the weights, in this way, the pivot point can be used to find the queue in a unique path.
           
             Pointqueue; queue
            
              Countqueue; // mark the path weight value, and define the coordinate points and temporary variables. // Count indicates the number of points of the same level in a single span. int flag = 3, Count = 1, count = 0; pointPos pointpos and pointtemp; // import the starting point coordinates into the queue and search pointqueue cyclically. push (start); while (! Pointqueue. empty () {pointpos = pointqueue. front (); pointqueue. pop (); if (Count <1 &&! Countqueue. empty () {Count = countqueue. front (); countqueue. pop () ;}if (0 = Count) continue; -- Count; // count records the number of directions in which each coordinate point has a path. int I, j; I = pointpos. x; j = pointpos. y; // cout <
             
               0 & I
              
                0 & j
               
                 0 & I
                
                  Max) {ni = I-1; nj = j; max = map [I-1] [j];} if (3 = map [I-1] [j]) break; if (map [I + 1] [j]> max) {ni = I + 1; nj = j; max = map [I + 1] [j];} if (3 = map [I + 1] [j]) break;} // determines if (j> 0 & j
                 
                   Max) {ni = I; nj = J-1; max = map [I] [J-1];} if (3 = map [I] [J-1]) break; if (map [I] [j + 1]> max) {ni = I; nj = j + 1; max = map [I] [j + 1];} if (3 = map [I] [j + 1]) break;} // cout <
                  
                    3) cout <0 <''; elsecout <
                   
                     0 & I
                    
                      W) {ni = I-1; w = map [I-1] [j];} if (map [I + 1] [j]> w) {ni = I + 1; w = map [I + 1] [j] ;}} if (j> 0 & j
                     
                       W) {nj = J-1; w = map [I] [J-1];} if (map [I] [j + 1]> w) {nj = j + 1; w = map [I] [j + 1] ;}} I = ni; j = nj; while (2! = Map [I] [j]) {// upper and lower judgment if (I> 0 & I
                      
                        0 & j
                       
                         3) cout <0 <''; elsecout <
                        
                         





Zookeeper

Related Article

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.