ZOJ 2849 Attack of Panda Virus

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2849

#include <iostream>
#include <queue>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define LINE_SIZE 510
int com[LINE_SIZE][LINE_SIZE];
struct Node{
    int x;
    int y;
    int day;
    int type;
};
bool cmp(Node a, Node b){
    return a.type < b.type;
}
vector<Node> virus;
priority_queue<Node> priQue;
int type[LINE_SIZE*LINE_SIZE];
bool operator < (const Node a, Node b){
    if (a.day != b.day){
        return a.day > b.day;
    }
    return a.type > b.type;
}
int movey[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
void bfs(){
    while (!priQue.empty()){
        Node front = priQue.top();
        priQue.pop();
        int flag = -1e7;
        for (int aim = 0; aim < 4; aim++){
            Node temp;
            temp.x = front.x + movey[aim][0];
            temp.y = front.y + movey[aim][1];
            if (com[temp.x][temp.y] < 0){
                if (0 - com[temp.x][temp.y] <= front.day){
                    temp.type = front.type;
                    temp.day = front.day;
                    type[front.type]++;
                    com[temp.x][temp.y] = front.type;
                    priQue.push(temp);
                }
                else{
                    if (flag < com[temp.x][temp.y])
                        flag = com[temp.x][temp.y];
                }
            }
        }
        if (flag != -1e7){
            front.day = flag*(-1);
            priQue.push(front);
        }
    }
}
int main(){
    int m, n;
    while (cin >> m >> n){
        memset(com, 0, sizeof(com));
        memset(type, 0, sizeof(type));
        while (!priQue.empty()){
            priQue.pop();
        }
        virus.clear();
        for (int i = 1; i <= m; i++){
            for (int j = 1; j <= n; j++){
                cin >> com[i][j];
                if (com[i][j] > 0){
                    Node temp;
                    temp.x = i;
                    temp.y = j;
                    temp.day = 0;
                    temp.type = com[i][j];
                    type[com[i][j]]++;
                    virus.push_back(temp);
                }
            }
        }
        sort(virus.begin(), virus.end(), cmp);
        for (int i = 0; i < virus.size(); i++){
            priQue.push(virus[i]);
        }
        bfs();
        int Q;
        cin >> Q;
        while (Q--){
            int nn;
            cin >> nn;
            cout << type[nn] << "\n";
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值