当前位置:网站首页>[31. Maze walking (BFS)]

[31. Maze walking (BFS)]

2022-07-23 16:57:00 Little silly bird_ coding

  • When asked When the shortest path , also The weight of the edge is 1 when Use BFS( Breadth first search .)

 Insert picture description here

Ideas

  • use g[n][m] Store maps ,d[n][m] Store starting point to n,m Distance of .

  • From the starting point, breadth first traverse the map .

  • When the map is traversed , The distance from the starting point to each point is calculated , Output d[n][m] that will do .

Figure explanation

 Insert picture description here
 Insert picture description here

Reasons for using queues

  • Because when you reach a point , There are four choices , We must walk in four directions at the same time , But the computer can only perform one step at a time , So put the directions to be processed into the queue in turn
  • Go to the corresponding point every time , Just take it out of the queue , Look in which direction

subject

 Insert picture description here

Method 1: Simulation queue

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;

typedef pair<int, int> PII;
 const int N = 110;
 
 int n, m;
 int g[N][N];  // Stored is a map 
 int d[N][N];  // Save the distance from this point to the starting point 
 
 PII q[N * N]; // Handwriting queue 
 


int bfs()
 {
    
    int hh = 0, tt = 0;    // Define team head and tail 
    q[0] = {
    0, 0};          // To initialize 
    memset(d, -1, sizeof(d)); // The distance is initialized to - 1 It means you haven't walked 
    d[0][0] = 0;             // It means the starting point has passed 
    
    int dx[4] = {
    -1, 0, 1, 0}, dy[4] = {
    0, 1, 0, -1}; // Definition x Vector sum of directions y Vector of direction 

    while (hh <= tt)  // The queue is not empty 
    {
    
    
      PII t = q[hh ++];  //  Take out the team leader element 
        
        for (int i = 0; i < 4; i ++)     // Enumerate four directions 
        {
    
            int x = t.first + dx[i], y =t.second + dy[i];   x Indicates the point you will reach when you walk in this direction 
            if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)// Within the boundary   And it's an open space for walking   And I haven't walked before 
            
            {
    
                d[x][y] = d[t.first][t.second] + 1;  // The distance to the starting point 
                q[ ++ tt] = {
    x, y};                 // New coordinates join the team 
            }
            
        }
    }
    return d[n - 1][m - 1];      Output the distance between the lower right corner and the starting point 
    
    
 }
 

 
 int main()
 {
    
     cin >> n >> m;
     for (int i = 0; i < n; i ++)
     {
    
         for (int j = 0; j < m; j ++)
         {
    
            cin >> g[i][j];
         }
     }
     
     cout << bfs() << endl;
     return 0;
 }

Method 2:STL

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int N = 110;

typedef pair<int, int> PII;

int n, m;
int g[N][N], d[N][N];

int bfs()
{
    
    queue< pair<int, int> > q;

    q.push({
    0, 0});

    memset(d, -1, sizeof(d));

    d[0][0] = 0;


    int dx[4] = {
    -1, 0, 1, 0}, dy[4] = {
    0, 1, 0, -1};

    while (q.size())// The queue is not empty 
    {
    
        PII t = q.front();// Take the team leader element 

        q.pop();// Out of the team 

        for (int i = 0; i < 4; i++)
        {
    
            int x = t.first + dx[i], y = t.second + dy[i];

            if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
            {
    
                d[x][y] = d[t.first][t.second] + 1;// The distance from the current point to the starting point 
                q.push({
    x, y});// New team 
            }
        }
    }

    return d[n - 1][m -1];
}

int main()
{
    
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> g[i][j];

    cout << bfs() << endl;

    return 0;
}


return d[n-1][m-1] Is the minimum path

  • When I first found this point ( The shortest distance ) This point is marked If you visit this point next time, you won't be able to visit ( So don't jump out of the loop when you find it )
  • After access is unavailable , The elements in the queue will pop up in turn , Exit the loop when the queue is empty .

Print path

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
PII q[N*N],Prev[N][N];         // Open more arrays , Record the previous point 
int g[N][N], d[N][N];
int n, m;
int bfs()
{
    
    int hh = 0, tt = 0;
    q[0] = {
    0, 0};
    memset(d, -1, sizeof d);

    int dx[4] = {
    -1, 0, 1, 0}, dy[4] = {
    0, 1, 0, -1};
    d[0][0] = 0;
    while(hh <= tt)
    {
    
        PII t = q[hh ++ ];
        for(int i = 0; i < 4; i ++ )
        {
    
            int x = dx[i] + t.first, y = t.second + dy[i];

            if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
            {
    
                d[x][y] = d[t.first][t.second] + 1;
                Prev[x][y] = t;                   // Store the previous point 
                q[++ tt] = {
    x, y};
            }
        }
    }
    int x = n - 1, y = m - 1;
    while(x || y)// One is not d be equal to 0
    {
    
        cout << x << ' ' << y << endl;          // Print the current point 
        PII t = Prev[x][y];                    // Look for the previous point , Continue printing 
        x = t.first, y = t.second;
    }

    return d[n - 1][m - 1];
}
int main()
{
    
    cin >> n >> m;
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < m;j ++)
            cin >> g[i][j];

    cout << bfs() << endl;

    return 0;
}


原网站

版权声明
本文为[Little silly bird_ coding]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231332175415.html