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.

ZOJ2849 Attack of Panda Virus 【广搜】【优先队列】_第1张图片

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:

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

Author:

XUE, Zaiyue

Source:

Zhejiang Provincial Programming Contest 2007

思路

使用优先队列进行广搜,天数小的在前,天数一样的type小的在前,注意每次上下左右搜完后把天数更新到周围level最小的点再入队,否则会TLE。

AC代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

const int MAX=500+10;

struct node_t
{
    int day;
    int type;
    int level;
    int vis;
    int x,y;
    bool friend operator < (node_t a,node_t b)
    {
        if(a.day==b.day)
        {
            return a.type>b.type;
        }
        return a.day>b.day;
    }
}node[MAX][MAX];

priority_queue <node_t> q;
int dx[]={-1,1,0,0},dy[]={0,0,-1,1};

int m,n;

int cnt[MAX*MAX]={0};
void bfs()
{
    while(!q.empty())
    {
        node_t u=q.top(); q.pop();
        int minL=0;
        bool first=1;
        for(int d=0 ; d<4 ; ++d)
        {
            int nx=u.x+dx[d] , ny=u.y+dy[d];
            if(nx>=0 && nx<m && ny>=0 && ny<n)
            {
                node_t &v=node[nx][ny];
                if(v.vis==0)
                {
                    if(u.day>=v.level)
                    {
                        v.vis=1;
                        v.type=u.type;
                        v.day=u.day;
                        cnt[v.type]++;
                        q.push(v);
                    }
                    else
                    {
                        if(first)
                        {
                            first=0;
                            minL=v.level;
                        }
                        else
                        {
                            minL=min(minL,v.level);
                        }
                    }
                }

            }
        }
        if(minL)
        {
            u.day=minL;
            q.push(u);//if still has some node to visit
        }
    }
}

int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(cnt,0,sizeof cnt);
        for(int i=0; i<m ; ++i)
        {
            for(int j=0 ; j<n ; ++j)
            {
                node_t &x=node[i][j];
                int t;
                scanf("%d",&t);
                x.x=i; x.y=j;
                if(t>0)
                {
                    x.day=1;
                    x.vis=1;
                    x.type=t;
                    cnt[t]++;
                    q.push(x);
                }
                else
                {
                    x.level=-t;
                    x.vis=0;
                }
            }
        }
        bfs();
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int t;
            scanf("%d",&t);
            printf("%d\n",cnt[t]);
        }
    }
    return 0;
}

你可能感兴趣的:(算法,ZOJ,优先队列,广搜)