ZOJ2849(Attack of Panda Virus)

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

//1832614 2009-04-13 20:27:57 Time Limit Exceeded  2849 C++ 3001 7320 Xredman 
//1835381 2009-04-16 18:55:38 Accepted  2849 C++ 920 8304 Xredman 
#include <iostream>
#include 
<queue>
#include 
<cstring>
using namespace std;

const int N = 502;

typedef 
struct node
ExpandedBlockStart.gifContractedBlock.gif
{
    
int x;
    
int y;
    
int day;
    
int ty;
    friend 
bool operator < (node a,node b)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(a.day != b.day)
            
return a.day>b.day;
        
else
            
return a.ty>b.ty;
    }

}
Point;

int m, n;
int cnt[N * N];
priority_queue
<Point> Q;
ExpandedBlockStart.gifContractedBlock.gif
int dir[4][2= {{0-1}{01}{-10}{10}};
int graph[N][N];

bool isBound(int x, int y)
ExpandedBlockStart.gifContractedBlock.gif
{
    
if(x < 0 || y < 0)
        
return false;
    
if(x >= m || y >= n)
        
return false;
    
return true;
}



void bfs()
ExpandedBlockStart.gifContractedBlock.gif
{
    Point a, b;
    
int tm;
    
while(!Q.empty())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        a 
= Q.top();
        Q.pop();
        tm 
= 1;
        
for(int i = 0; i < 4; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            b.x 
= a.x + dir[i][0];
            b.y 
= a.y + dir[i][1];
            
if(isBound(b.x, b.y) && graph[b.x][b.y] < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if(graph[b.x][b.y] + a.day >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    b.ty 
= a.ty;
                    cnt[b.ty]
++;
                    b.day 
= a.day;
                    Q.push(b);
                    graph[b.x][b.y] 
= b.ty;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if(tm == 1 || graph[b.x][b.y] > tm)
                        tm 
= graph[b.x][b.y];
                }

            }

        }

        
if(tm != 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            a.day 
= -tm;
            Q.push(a);
        }

    }

}



int main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
int i, j, T, q;
    Point cc;
    
int tmp;
    
while(cin>>m>>n)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        memset(cnt, 
0sizeof(cnt));
        
while(!Q.empty())
            Q.pop();
        
for(i = 0; i < m; i++)
            
for(j = 0; j < n; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                scanf(
"%d"&tmp);
                graph[i][j] 
= tmp;
                
if(tmp >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    cnt[tmp]
++;
                    cc.x 
= i; cc.y = j;
                    cc.ty 
= tmp; cc.day = 1;
                    Q.push(cc);
                }

            }

            bfs();
            cin
>>T;
            
while(T--)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cin
>>q;
                cout
<<cnt[q]<<endl;
            }

    }

    
    
return 0;
}

转载于:https://www.cnblogs.com/Xredman/archive/2009/04/16/1437448.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值