zoj2849Attack of Panda Virus

来源:互联网 发布:hp网络交换机 编辑:程序博客网 时间:2024/06/06 19:43

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 this virus difficult to handle is that it has many variations.

Unfortunately, our lab's network was also infected with the Panda Virus. As you can see from the above diagram, the computers in our lab are placed in a matrix of M rows and N columns. A computer is only connected with the computers next to it. At the beginning, Tcomputers 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:

  1. The virus can only spread along the network from the already infected computers to the clean ones.
  2. If a computer has already been infected by one virus variation, it will never be infected by another variation.
  3. 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.
  4. 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 <= MN <= 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

优先队列+BFS

#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<cstring>using namespace std;struct node{    int day,type,x,y;    friend bool operator< (node A,node B)    {        if(A.day!=B.day)            return A.day>B.day;        else            return A.type>=B.type;    }};priority_queue<node>q;int m,n,i,j,a[600][600],num[250010];const int minn=-0x3f3f3f3f;void bfs(){    int xx[4]={1,-1,0,0};    int yy[4]={0,0,1,-1};    int newx,newy,t=0;    node node2;    while(!q.empty())    {        node2=q.top();        int flag=minn;        q.pop();        for(i=0;i<4;i++)        {            newx=node2.x+xx[i];            newy=node2.y+yy[i];            if(newx>=0&&newx<m&&newy>=0&&newy<n&&a[newx][newy]<0)            {                if(a[newx][newy]+node2.day>=0)                {                    node node3;                    a[newx][newy]=node2.type;                    node3.type=node2.type;node3.day=node2.day;                    node3.x=newx;node3.y=newy;                    num[node3.type]++;                    q.push(node3);                }else                {                    if(a[newx][newy]>flag)                        flag=a[newx][newy];                }            }        }        if(flag!=minn)        {            node2.day=(-1)*flag;            q.push(node2);        }    }}int main(){    while(~scanf("%d %d",&m,&n))    {        memset(num,0,sizeof(num));        for(i=0;i<m;i++)        {            for(j=0;j<n;j++)            {                scanf("%d",&a[i][j]);                if(a[i][j]>0)                {                    node node1;                    node1.type=a[i][j];num[node1.type]++;                    node1.day=1;                    node1.x=i;                    node1.y=j;                    q.push(node1);                }            }        }        int t;        scanf("%d",&t);        bfs();        for(i=0;i<t;i++)        {            int type1;            scanf("%d",&type1);            printf("%d\n",num[type1]);        }    }    return 0;}

原创粉丝点击