DFS and BFS solution to a problem: [kaungbin] you fly with simple problem-solving search report

DFS and  BFS

Before we generally talk about solving dfs and bfs of. (I feel I will not bfs)

1.DFS

dfs (depth-first algorithm) as its name, dfs is considerable depth, not to come to the deepest kind of never looked back.

Depth-first search is an enumeration of all the full path to traverse search method in all cases.

The use recursion can realize the depth-first search.

When using recursion, the system will call a thing called the system stack to store the state of recursion in each layer, so using recursion to implement DFS in nature but it is still stack.

 

DFS following template:

void DFS () {   // parameter indicates the state of 
    IF (reaches the end state) {   
        ... // Add meaning of the questions   
        return ;   
    }   
    IF (out of range or is not legal state)  
         return ;  
     IF (special state) // Shear branch 
        return ;
     for (extended mode) {  
         iF (extended state legitimate manner achieved) {   
            modify operation; // add meaning of the questions   
            marks;   
            DFS ();  
            (marked reduction);   
            // if the meaning of the questions marked reduction  
             // If add (marked reduction) is backtracking   
        }   
    }   
}  

Then start today's solution to a problem

Title from https://vjudge.net/problem/POJ-3984

Maze

1. Description Title: 2

The definition of a two-dimensional array:


int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};


It represents a labyrinth, in which 1 represents the wall, 0 way to go, can only go sideways or vertically to go, can not go sideways, requiring programmed to find the shortest route from upper left to lower right corner

Input

A 5 × 5 two-dimensional array, showing a maze. Ensure data has a unique solution.

Output

Left to bottom right shortest path, the format as shown in the sample.

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0) 
(1, 0) 
(2, 0) 
(2, 1) 
(2, 2) 
(2, 3) 
(2, 4) 
(3, 4) 
(4, 4) 


2. Analysis of the subject
which it belongs to the basic questions of the title search, that is, to give you a map, allowing you to find the shortest path on the map.
in the search problem, we can use the above mentioned dfs (depth-first algorithm), there is a species is bfs (breadth-first algorithm). Both methods can be.
However, this question is of the order of traversal of the labyrinth, so dfs not easy to use, so we choose BFS, by using a queue structure to store the path.


Here is the code:
#include <stdio.h>
 struct Node {
     int X; // X coordinate 
    int Y; // Y coordinate 
    int pre; // to point of departure 
};
 int Book [ . 6 ] [ . 6 ];   // to record whether the access point through 
int Map [ . 6 ] [ . 6 ];    // record FIG 
struct Node queue [ 20 is ]; // queue storage path 
void Print ( struct Node a) // implemented function output path 
{
     IF (a.pre == - 1 )  
    {
        printf("(%d, %d)\n",a.x,a.y);
        return ;
    }
    else
    {
        print(queue[a.pre]);
        printf("(%d, %d)\n",a.x,a.y);
    }
}
int main()
{
    int i,j,k,m,n,x,y;
    int head,tail;
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            scanf("D% " , & Map [I] [J]); 
        } 
    } 
    head = 0 ; 
    tail = 0 ; 
    Queue [tail] .x = 0 ; 
    Queue [tail] .y = 0 ; 
    Queue [tail] .PRE = - . 1 ; 
    Book [ 0 ] [ 0 ] = . 1 ; 
    tail ++ ;
     the while (head <tail) // when the queue is empty out, the search does not find a feasible path described 
    {
         int Next [ . 4 ] [ 2 ] = {{ 0, 1 }, { 0 - 1 }, { 1 , 0 }, {- 1 , 0 }}; // define four directions 
        int In Flag = 0 ;
         for (I = 0 ; I < . 4 ; I ++)   / / around to explore from the current point 
        {
             int nextx = Queue [head] Next .x + [I] [ 0 ];
             int nexty = Queue [head] Next .y + [I] [ . 1 ]; // for mobile 
            IF ( nextx < 0 || nextx> . 5 || nexty < 0 || nexty>5 ) // beyond the boundaries of the bounce 
            {
                 Continue ; 
            } 
            IF (Book [nextx] [nexty] == 0 && Map [nextx] [nexty] == 0 ) // when the node is not accessed and only the feasible point team 
            { 
                Book [nextx] [nexty] = . 1 ; 
                Queue [tail] .x = nextx; 
                Queue [tail] .y = nexty; 
                Queue [tail] .PRE = head; 
                tail ++ ; 
            } 
            IF (nextx == . 4 nexty == && 4) // reached the destination, no doubt out of the end of the cycle 
            { 
                In Flag = . 1 ;
                 BREAK ; 
            } 
        } 
        IF (In Flag) // arrival calling function output path 
        { 
            Print (Queue [tail - . 1 ]);
             BREAK ; 
        } 
        head ++; // dequeue 
    }
     return  0 ; 
}

The main attention is to use the queue, others are ok

                                                                                     

 

Guess you like

Origin www.cnblogs.com/sidenynb/p/12228989.html
Recommended