zoukankan      html  css  js  c++  java
  • [bfs+最短路+小顶堆] PUBG

    PUBG (nowcoder.com)icon-default.png?t=LA92https://ac.nowcoder.com/acm/problem/15752

    题目描述

    最近,喜爱ACM的PBY同学沉迷吃鸡,无法自拔,于是又来到了熟悉的ERANGEL。经过一番搜寻,PBY同学准备动身前往安全区,但是,地图中埋伏了许多LYB,PBY的枪法很差,希望你能够帮他找到一条路线,每次只能向上、下、左、右移动,尽可能遇到较少的敌人。

    输入描述:

    题目包含多组测试,请处理到文件结束;
    第一行是一个整数n,代表地图的大小;
    接下来的n行中,每行包含n个整数a,每个数字a代表当前位置敌人的数量;
    1 < n <= 100,1 <= a <= 100,-1代表当前位置,-2代表安全区。

    输出描述:

    对于每组测试数据,请输出从当前位置到安全区所遇到最少的敌人数量,每个输出占一行。

    示例1

    输入

    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

    输出

    9

    示例2

    输入

    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

    输出

    173

    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    const int N = 110;
    int n, sx, sy;
    int mp[N][N], st[N][N];
    int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1};//偏移量
    
    struct site{
        int x, y, dist;
        bool operator <(const site& b)const{
            return dist > b.dist;
        }
    };
    
    void bfs()
    {
        priority_queue<site> q;
        q.push({sx, sy, 0});
        st[sx][sy] = 1;
        while(q.size())
        {
            site s = q.top();
            q.pop();
            
            for(int i = 0; i < 4; i ++){
                int nx = s.x+dx[i], ny = s.y+dy[i];
                if(nx && ny && nx<=n && ny<=n && !st[nx][ny]){
                    if(mp[nx][ny]==-2){
                        cout << s.dist << endl;
                        return ;
                    }else{
                        q.push({nx, ny, s.dist+mp[nx][ny]});
                        st[nx][ny] = 1;
                    }
                }
            }
        }
    }
    int main()
    {
        while(cin>>n)
        {
            memset(st,0,sizeof st);
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    scanf("%d",&mp[i][j]);
                    if(mp[i][j]==-1) sx=i, sy=j;
                }
            }
            bfs();
        }
        
        return 0;
    }

    本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799034.html

  • 相关阅读:
    LeetCode(287)Find the Duplicate Number
    LeetCode(290) Word Pattern
    LeetCode(205)Isomorphic Strings
    LeetCode(201) Bitwise AND of Numbers Range
    LeetCode(200) Number of Islands
    LeetCode(220) Contains Duplicate III
    LeetCode(219) Contains Duplicate II
    命令行执行Qt程序
    LeetCode(228) Summary Ranges
    redis 的安装和使用记录
  • 原文地址:https://www.cnblogs.com/Knight02/p/15799034.html
Copyright © 2011-2022 走看看