The difference between BFS and DFS BFS realizes the shortest path of the maze

Share the artificial intelligence tutorial of my teacher! Zero foundation, easy to understand! http://blog.csdn.net/jiangjunshow

You are also welcome to reprint this article. Share knowledge, benefit the people, and realize the great rejuvenation of our Chinese nation!

                       

BFS can find the shortest path, because each time BFS is performed is equivalent to the current path length. For an N*N matrix, BFS runs at most n*n times.

Depth-first search is equivalent to one person walking a maze, and wide search is equivalent to infinite people walking in different directions (because there are people walking on each road at the same time).
DFS is equivalent to a push-down stack. It is the principle of first-in, last-out (if you can't find it, go back until you get back to the place where there is a way) (DFS uses the stack implicitly)
BFS uses the queue to search according to the distance from the starting point.

BFS uses a queue to hold vertices that have been marked but whose adjacency list has not yet been visited. First add the starting point to the queue, then repeat the following operations until the queue is empty.
1. Take the next vertex V in the queue and mark it
2. Add all unmarked vertices adjacent to V to the queue.

The mark edgeto of BFS represents its previous node, and its previous node must have only one situation, which represents the shortest solution (not necessarily unique), because the points radiating from 1, 1 are constantly brushing the shortest path .

The depth-first search first finds the vertex farthest from the starting point, and the breadth-first search first finds the nearest vertex. The
depth first search must find a connected component (because it reaches a dead end)

HDOJ1240
BFS shortest path

#include <iostream>#include <queue>#include <string>using namespace std;int n;const int size =12;int matrix[size][size][size];int step[size][size][size];int sx,sy,sz,dx,dy,dz;int tx[]={
    
    1,-1,0,0,0,0};int ty[]={
    
    0,0,1,-1,0,0};int tz[]={
    
    0,0,0,0,-1,1};bool check(int x,int y,int z){    if(matrix[x][y][z]==0) return false;    return true;;}typedef struct{    int x,y,z;}Point;int bfs(int x,int y,int z){    step[x][y][z]=0;    matrix[x][y][z]=0;    queue<Point> Q;    Point cur;    cur.x=x;    cur.y=y;    cur.z=z;    Q.push(cur);    while(!Q.empty()){        cur = Q.front();        Q.pop();        Point next;        if(cur.x==dx&&cur.y==dy&&cur.z==dz) return step[cur.x][cur.y][cur.z];        for(int i=0;i<6;i++){            next=cur;            next.x+=tx[i];            next.y+=ty[i];            next.z+=tz[i];            step[next.x][next.y][next.z]=step[cur.x][cur.y][cur.z]+1;            if(check(next.x,next.y,next.z)){                matrix[next.x][next.y][next.z]=0;                Q.push(next);            }        }    }    return -1;}int main(int argc, char const *argv[]){    string temp;    char c;    while(cin>>temp>>n){        memset(matrix,0,sizeof(matrix));//0不能走        for(int i=1;i<=n;i++){            for(int j=1;j<=n;j++){                for(int k=1;k<=n;k++){                    cin>>c;                if(c=='X') matrix[i][j][k]=0;                else matrix[i][j][k]=1;                     }            }        }        cin>>sx>>sy>>sz>>dx>>dy>>dz;        sx++,sy++,sz++,dx++,dy++,dz++;        cin>>temp;        matrix[sx][sy][sz]=matrix[dx][dy][dz]=1;        int ans = bfs(sx,sy,sz);        if(ans>=0) cout<<n<<" "<<ans<<endl;        else cout<<"NO ROUTE"<<endl;    }    return 0;}
  
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

There are many gains from the code copied by others:
1. It is very convenient to use tx, ty, tz arrays to simulate the search in the third direction.
2. BFS is done with loops instead of recursion, put the first point in, and then you can loop continuously until the queue is empty.
3. The pop of the queue API does not return the popped element, you must use front

Attachment: Simple operation of stack and queue:

1.
The definition of the stack stack template class is in the header file.
The stack template class requires two template parameters, one is the element type and the other is the container type, but only the element type is
necessary . When no container type is specified, the default container type is deque.
The sample code for defining the stack object is as follows:
stack s1;
stack s2;
The basic operations of stack are : push into the
stack, such as: s.push(x);
pop, such as : s.pop(); Just removes the top element of the stack, and does not return that element.
Access the top of the stack, such as: s.top() to
judge the stack is empty, such as: s.empty(), when the stack is empty, return true.
Access the number of elements in the stack, for example: s.size().

2.
The definition of the queue queue template class is in the header file.
Similar to the stack template class, the queue template class also requires two template parameters, one is the element type and the other is the container
type . The element type is required, the container type is optional, and the default is deque type.
Example code to define a queue object is as follows:
queue q1;
queue q2;

The basic operations of queue are:
enter the queue, such as: q.push(x); Connect x to the end of the queue.
Dequeue, for example: q.pop(); Pop the first element of the queue, note that the value of the popped element will not be returned.
Access the first element of the queue, such as: q.front(), which is the earliest element that was pushed into the queue.
Access the rear element, such as: q.back(), which is the last element pushed into the queue.
Judging that the queue is empty, for example: q.empty(), when the queue is empty, return true.
Access the number of elements in the queue, such as: q.size()

           

Call my teacher's artificial intelligence tutorial! http://blog.csdn.net/jiangjunshow

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324149336&siteId=291194637