听听 艾薇儿 girlfriend

zoj 2849 Attack of Panda Virus

ZOJ Problem Set - 2849
Attack of Panda Virus

Time Limit: 3 Seconds      Memory Limit: 32768 KB

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, 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:

  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 <= 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

Author: XUE, Zaiyue


Source: Zhejiang Provincial Programming Contest 2007
Submit    Status
//1831249 2009-04-12 16:31:01 Time Limit Exceeded  2849 C++ 3001 1168 Wpl 
//1831259 2009-04-12 16:36:41 Time Limit Exceeded  2849 C++ 3001 1168 Wpl 
//1831261 2009-04-12 16:39:54 Time Limit Exceeded  2849 C++ 3001 1168 Wpl 
//1831422 2009-04-12 18:11:22 Time Limit Exceeded  2849 C++ 3001 4248 Wpl 
//1831444 2009-04-12 18:46:41 Time Limit Exceeded  2849 C++ 3001 5232 Wpl 
//1835355 2009-04-16 18:01:39 Accepted  2849 C++ 1150 8304 Wpl
//1835403 2009-04-16 19:19:50 Accepted  2849 C++ 880 8304 Wpl  
#include <iostream>
#include 
<queue>
#include 
<cmath>
#define MAX 502
using namespace std;
typedef 
struct node
{
    
int x;
    
int y;
    
int day;
    
int ty;
    friend 
bool operator < (node a,node b)
    {
        
if(a.day!=b.day)
            
return a.day>b.day;
        
else
            
return a.ty>b.ty;
    }
}Point;
int maze[MAX][MAX],n,m,sum,a[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int num[MAX*MAX];
priority_queue
<Point>Q;
Point p,temp;
void Init()
{
    
while(!Q.empty())
            Q.pop();
    
int i,j;
    sum
=0;
    
for(i=0;i<MAX*MAX;i++)
        num[i]
=0;
    
for(i=0;i<n;i++)
        
for(j=0;j<m;j++)
        {
            scanf(
"%d",&maze[i][j]);
            
if(maze[i][j]>0)
            {
                p.x
=i;
                p.y
=j;
                p.day
=1;
                p.ty
=maze[i][j];
                num[p.ty]
++;
                Q.push(p);
                sum
++;
            }
        }
}
bool Bound(int x,int y)
{
    
if(x>=0&&y>=0&&x<n&&y<m)
        
return 1;
    
else
        
return 0;
}
void BFS()
{
    
int i,tt;
    
while(!Q.empty())  //队列空,和全部遍历均需结束
    {
        p
=Q.top();
        Q.pop();
        tt
=1;
        
for(i=0;i<4;i++)
        {
            temp.x
=p.x+a[i][0];
            temp.y
=p.y+a[i][1];
            temp.day
=p.day;
            temp.ty
=p.ty;
            
if(!Bound(temp.x,temp.y))
                
continue;
            
if(maze[temp.x][temp.y]<0)
            {
                
if(p.day>=abs(maze[temp.x][temp.y]))
                {
                    maze[temp.x][temp.y]
=p.ty;
                    sum
++;
                    num[p.ty]
++;
                    
if(sum==n*m)
                        
return ;
                    Q.push(temp);
                }
                
else
                {
                    
if(maze[temp.x][temp.y]>tt||tt==1)
                        tt
=maze[temp.x][temp.y];
                }
            }
        }
        
if(tt!=1)
        {
            p.day
=-tt;
            Q.push(p);
        }
    }    
}
int main()
{
    
while(scanf("%d%d",&n,&m)!=EOF)
    {
        
int i,j,Q,type,s;
        Init();
        BFS();
        scanf(
"%d",&Q);
        
while(Q--)
        {
            scanf(
"%d",&type);
            printf(
"%d\n",num[type]);
        }
    }
    
return 0;
}
posted @ 2009-05-08 13:45  往往  阅读(677)  评论(0编辑  收藏  举报