In 2018, the 10th Beijing University of Information Science and Technology Programming Competition and ACM Selection Competition A PUBG (bfs seeks the shortest path)

Link: https://www.nowcoder.com/acm/contest/118/A
Source: Niuke.com

PUBG
Time limit: C/C++ 1 second, other languages ​​2 seconds
Space limit: C/C++ 32768K, other languages ​​65536K
64bit IO Format: %lld

Topic description

Recently, PBY classmates who love ACM are addicted to eating chicken and can't extricate themselves, so they came to the familiar ERANGEL again. After some searching, PBY is ready to leave for the safe area. However, there are many LYBs ambushed on the map. PBY's marksmanship is very poor. I hope you can help him find a route. Each time he can only move up, down, left and right, Encounter as few enemies as possible.

Enter description:

The question contains multiple sets of tests, please process until the end of the file; 
the first line is an integer n, representing the size of the map; in the
next n lines, each line contains n integers a, each number a represents the number of enemies at the current location ;
1 < n <= 100, 1 <= a <= 100, -1 for current position, -2 for safe zone.

Output description:

For each set of test data, please output the minimum number of enemies encountered from the current position to the safe zone, one line per output.
Example 1

enter

5
6 6 0 -2 3
4 2 1 2 1
2 2 8 9 7
8 1 2 1 -1
9 7 2 1 2

output

9
Example 2

enter

5
62 33 18 -2 85
85 73 69 59 83
44 38 84 96 55
-1 11 90 34 50
19 73 45 53 95

output

173 

My method is bfs, similar to spfa
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include <queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<deque>
#include<algorithm>
#include<iomanip>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int a[105][105];
int v[105][105];
int s[105][105];
int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node
{
    int x,y;
};
queue<node>q;
int main()
{
    int n;
    int si, sj;
    int ei, ej;
    while (cin >> n)
    {
        int min = inf;
        memset(s,inf,sizeof(s));
        memset(v,0,sizeof(v));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>a[i][j];
                if(a[i][j]==-1)
                {
                    si = i;
                    sj=j;
                    a[i][j]=0;
                }
                if(a[i][j]==-2)
                {
                    ei = i;
                    ej = j;
                    a[i][j]=0;
                }
            }
        }
        node c;
        c.x=si;
        c.y=sj;
        v [si] [sj] = 1 ;
        s[si][sj]=0;
        q.push(c);
        // Somewhat similar to spfa 
        while (! q.empty())
        {
            node c=q.front();
            q.pop();
            v[cx][cy] = 0 ; // The mark is not in the queue, because it may be updated later, it will be better 
            for ( int i= 0 ;i< 4 ;i++ )
            {
                int xx=c.x+d[i][0];
                int yy=c.y+d[i][1];
                if(xx<1||yy<1||xx>n||yy>n) continue;
                if(s[c.x][c.y]+a[xx][yy]<s[xx][yy])
                {
                    s[xx][yy]=s[c.x][c.y]+a[xx][yy];
                    if(!v[xx][yy])
                    {
                        v[xx][yy]=1;
                        node h;
                        h.x=xx;
                        h.y=yy;
                        q.push(h); // Updated, put it in the queue if it is not in the queue 
                    }
                }

            }
        }
        cout<<s[ei][ej]<<endl;
    }
    return 0;
}
 
      

 



Guess you like

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