A computer is only connected with the computers next to it. At the beginning, T computers were infected with the Panda Virus, each with a different variation (Type 1, Type 2… Type T). Each computer in the network has a specific defense level L (0 < L < 1000). The Panda Virus will rapidly spread across the network according to the following rules:
The virus can only spread along the network from the already infected computers to the clean ones.
If a computer has already been infected by one virus variation, it will never be infected by another variation.
The transmission capacity of the Panda Virus will increase each day. In day 1, the virus only infects computers with a defense level 1 provided the virus can spread to that computer, however, a computer with a defense level >1 will 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 is 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
So at last, all the computers in the networks were infected by virus.
Your task is to calculate after all the computers are infected, how many computers are infected with some specific virus variations.
Input
The input contains multiple test cases!
On the first line of each test case are two integers M and N (1 <= M, N <= 500), followed by a M * N matrix. A positive integer T in the matrix indicates that the corresponding computer had already been infected by the virus variations of type T at the beginning while a negative integer -L indicates that the computer has a defense level L. Then there is an integer Q indicating the number of queries. Each of the following Q lines has an integer which is 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 type 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
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
using namespace std;
int a[505][505];
struct node
{
int day,x,y,ty;
bool operator <(const node &a)const
{
if(a.day!=day)
return day>a.day;
return ty>a.ty;
}
} tmp;
int n,m;
int dir[4][2]= {1,0, -1, 0, 0,1 , 0,-1};
int cnt[505*505];
int main()
{
while(~scanf("%d%d",&n,&m))
{
priority_queue<node> q;
memset(cnt,0,sizeof(cnt));
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]>0)
{
tmp.day=1;
tmp.ty=a[i][j];
tmp.x=i;
tmp.y=j;
q.push(tmp);
cnt[a[i][j]]++;
}
}
while(!q.empty())
{
node cur=q.top();
q.pop();
int tttt=-0x3f3f3f3f;
for(int i=0;i<4;i++)
{
tmp.x=cur.x+dir[i][0];
tmp.y=cur.y+dir[i][1];
if(tmp.x<0||tmp.x>=n) continue;
if(tmp.y<0||tmp.y>=m) continue;
if(a[tmp.x][tmp.y]>0) continue;
if(a[tmp.x][tmp.y]+cur.day>=0)
{
tmp.ty=cur.ty; //level
tmp.day=cur.day;
q.push(tmp);
cnt[tmp.ty]++; //num of virus
a[tmp.x][tmp.y]=tmp.ty; //reset virus
}
else
{
if(tttt<a[tmp.x][tmp.y])
tttt=a[tmp.x][tmp.y];
}
}
if(tttt!=-0x3f3f3f3f)
{
cur.day=tttt*-1;
q.push(cur);
}
}
int qq;
scanf("%d",&qq);
while(qq--)
{
int x;
scanf("%d",&x);
printf("%d\n",cnt[x]);
}
}
}