Attack of Panda Virus
time limit (normal/java): 3000ms/30000ms Run memory limit: 65536KByte
Description
In recent months, a computer virus spread across networks in China. The virus came with an icon of a lovely panda, hence the name Panda virus. What makes the virus difficult to handle is the it has many variations.
Unfortunately, our lab's network is also infected with the Panda Virus. As can see from the above diagram, the computers with our lab is placed in a matrix OfM rows andn columns. A computer is only connected with the computers next to it. At the beginning,t computers were infected and the Panda Virus, each with a different variation (type 1, type 2 ... Typet). Each computer in the network has a specific defense Levell (0 <l < 1000). The Panda Virus would rapidly spread across the network according to the following rules:the Virus can only spread along t He network from the already infected computers to the clean ones. If a computer have already been infected by one virus variation, it'll never be infected by another variation. The transmission capacity of the Panda Virus would increase each day. In Day 1, the virus is infects computers with a defense level 1 provided the virus can spread to that computer, however, A computer with a defense level >1 would stop the TransmissIon along that path. In day D, it can spread to all the computers connected with a defense level <=d, provided that the transmission are not Stopped by a computer with a defense level >d along the path. Within one day, the virus variation of type 1 would spread first and infects all the computers it can reach. And then the virus variation of type 2, then type 3, etc.
The following samples show the infection process described above:
At the beginning, only 2 computers were infected:
1 0 0 0
0 0 0 2
0 0 0 0
In Day 1:
1 0 0 0
0 0 0 2
0 0 2 2
In Day 2:
1 0 1 0
1 1 1 2
0 1 2 2
In Day 3:
1 1 1 1
1 1 1 2
1 1 2 2
The computers in the networks were infected by virus.
Your task is to calculate after all the computers was infected, how many computers was infected with some specific virus V Ariations.
input
The input contains multiple test cases!
On the first line of all test case is INTEGERSM andn (1 <=m, n <=), followed by a M * N matrix. A positive Integert in the matrix indicates so the corresponding computer had already been infected by the virus Variati ONS of Typet at the beginning and a negative integer-l indicates that the computer have a defense Levell. Then there was an INTEGERQ indicating the number of queries. Each of the FOLLOWINGQ lines have an integer which are the virus variation type we care.
Output
For each query of the input, output an integer in a single line which indicates the number of computers attacked by this T Ype of virus variation.
Sample Input
3 4
1-3 -2-3
-2-1-2 2
-3-2 -1-1
2
1
2
Sample Output
9
3
Day is not the same day small in front of the same type small in front if there is no infected computer around the current point of your current point of day update to the surrounding level of the smallest in the incoming queue
#include <stdio.h> #include <string.h> #include <queue> using namespace std;
const int MAX = 510;
struct Node {int day;
int type;
int x;
int y;
BOOL Friend operator < (node A,node b) {if (a.day! = b.day) return a.day > B.day;
return a.type > B.type;
}
};
Priority_queue<node> Q;
int n,m;
int Cnt[max*max];
int A[max][max];
int dir[4][2] = {0,1,0,-1,1,0,-1,0};
void BFs () {int i;
while (!q.empty ()) {int flag = 0;
Node P = q.top ();
Q.pop ();
for (i = 0;i < 4; i++) {node T;
T.x = p.x + dir[i][0];
T.y = p.y + dir[i][1];
if (t.x >= 1 && t.x <= n && t.y >= 1 && t.y <= m && A[t.x][t.y] < 0) {
if (P.day >= a[t.x][t.y] * ( -1)) {t.type = P.type;
T.day = P.day;
A[T.X][T.Y] = P.type;
Q.push (t);
cnt[p.type]++;
} else {if (A[t.x][t.y] > Flag | |!flag) flag = A[t.x][t.y]; }}} if (flag) {p.day =-flag;
Q.push (P);
}}} int main () {int i,j,k,t;
node x;
while (scanf ("%d%d", &n,&m)!=eof) {while (!q.empty ()) Q.pop ();
memset (cnt,0,sizeof (CNT));
for (i = 1;i <= n; i++) {for (j = 1;j <= m; j + +) {scanf ("%d", &a[i][j]);
if (A[i][j] > 0) {x.x = i;
x.y = j;
X.type = A[i][j];
X.day = 1;
cnt[a[i][j]]++;
Q.push (x);
}}} BFS ();
scanf ("%d", &k);
while (k--) {scanf ("%d", &t);
printf ("%d\n", cnt[t]);
}} return 0; }