您的位置:首页 > 其它

ZOJ 2849 Attack of Panda Virus

2016-02-19 20:16 423 查看
题目链接: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";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: