ZOJ--2849 Attack of Panda Virus

在一个电脑网络中有n*m台电脑,网络中有病毒,标号为正数,负数则表示该电脑最早可以被感染病毒的天数。标号小的病毒先感染。求最后各个病毒都感染了多少台电脑。

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1849

搜索。用一个优先队列。优先队列里面存储坐标、被感染的病毒编号和第几天被感染。

优先选取先感染的点,若感染的时间一样,就选取病毒标号小的点。

#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define pb push_back
#define MP make_pair

typedef long long ll;

const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int maxn = 510;

int dxy[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
struct HeapNode{
    int x, y, type, day;
    HeapNode(){}
    HeapNode(int _x, int _y, int _type, int _day):x(_x), y(_y), type(_type), day(_day){ }
    bool operator < (const HeapNode& t) const{
        return (day > t.day) || (day == t.day && type > t.type);
    }
};
int a[maxn][maxn], vis[maxn][maxn], ans[maxn * maxn], t[maxn][maxn];
int n, m, q;
int main(){
    while(scanf("%d%d", &n, &m) == 2){
        memset(vis, 0x3f, sizeof(vis));
        memset(ans, 0, sizeof(ans));
        memset(t, 0x3f, sizeof(t));
        priority_queue<HeapNode> Q;
        while(!Q.empty()) Q.pop();
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                scanf("%d", &a[i][j]);
                if(a[i][j] > 0){
                    Q.push(HeapNode(i, j, a[i][j], 0));
                    vis[i][j] = a[i][j];
                    t[i][j] = 0;
                }

            }
        }
        while(!Q.empty()){
            HeapNode now = Q.top(); Q.pop();
            if(a[now.x][now.y] < 0 && (vis[now.x][now.y] < now.type || t[now.x][now.y] < now.day)) continue;//若该点已被其他标号更小的病毒感染
            for(int i=0; i<4; i++){
                int xx = now.x + dxy[i][0];
                int yy = now.y + dxy[i][1];
                if(xx < 0 || xx >= n || yy < 0 || yy >= m || a[xx][yy] > 0) continue;
                int d = max(-a[xx][yy], now.day);//若被当前点感染的感染天数
                if(t[xx][yy] < d) continue;
                if(t[xx][yy] == d){
                    if(vis[xx][yy] <= now.type) continue;
                    vis[xx][yy] = now.type;
                    Q.push(HeapNode(xx, yy, now.type, d));
                }
                else{
                    t[xx][yy] = d;
                    vis[xx][yy] = now.type;
                    Q.push(HeapNode(xx, yy, now.type, d));
                }

            }
        }
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            ans[ vis[i][j] ]++;
        scanf("%d", &q);
        int x;
        while(q--){
            scanf("%d", &x);
            printf("%d\n", ans[x]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值