【ZOJ2849】Attack of Panda Virus(优先队列+bfs)

题目链接

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

【题意】

有一个名叫“熊猫烧香”的病毒,它在网络上的感染规则是:

1.病毒只能从已感染的电脑传给未感染的电脑

2.一个电脑只能被感染一次

3.病毒只能感染防御等级低于或等于他的感染能力的电脑,病毒的感染能力每天+1 

4.病毒按种类先小后大顺序进行感染

5.只能感染相邻的电脑(上下左右四个方向)

然后求当所有电脑被感染之后,每种类型的病毒的感染电脑台数。

【解题思路】

用优先队列存储已感染的电脑然后进行bfs对上下左右4个方向进行搜索,若病毒的等级比电脑的防御等级高,则该电脑被感染,并将该电脑的状态更新加入优先队列,若病毒的等级比电脑的防御等级低,则找一个该病毒周围最少所需天数能感染到的电脑,直到所有电脑都被感染。

这里要用优先队列的自定义排序函数,先按天数从小到大排,若天数相等则按照病毒类型排,不是很会qaq,一会再学一下。

https://www.cnblogs.com/star-and-me/p/8681434.html

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=505;
const int INF=0x3f3f3f;
int dirx[]={1,-1,0,0};
int diry[]={0,0,1,-1};
int p[maxn][maxn],cnt[250005],n,m;
struct node
{
    int x,y,type,level;
    friend bool operator <(const node& a,const node& b)
    {
        return (a.level!=b.level)?a.level>b.level:a.type>b.type;
    }
}a;
priority_queue<node>pq;
void bfs()
{
    while(!pq.empty())
    {
        int mink=INF;
        node t=pq.top();
        pq.pop();
        for(int i=0;i<4;i++)
        {
            a.x=t.x+dirx[i];
            a.y=t.y+diry[i];
            if(a.x>=0 && a.x<n && a.y>=0 && a.y<m && p[a.x][a.y]<0)
            {
                if(t.level+p[a.x][a.y]>=0)
                {
                    a.type=t.type;
                    a.level=t.level;
                    pq.push(a);
                    cnt[a.type]++;
                    p[a.x][a.y]=t.type;
                }
                else mink=min(mink,abs(p[a.x][a.y]));
            }
        }
        if(mink!=INF)
        {
            t.level=mink;
            pq.push(t);
        }
    }
}
int main()
{
    int num=0,k;
    while(~scanf("%d%d",&n,&m))
    {
        memset(p,0,sizeof(p));
        memset(cnt,0,sizeof(cnt));
        while(!pq.empty())pq.pop();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&p[i][j]);
                if(p[i][j]>0)
                {
                    a.x=i;
                    a.y=j;
                    a.type=p[i][j];
                    cnt[a.type]++;
                    a.level=1;
                    pq.push(a);
                }
            }
        }
        bfs();
        int x;
        scanf("%d",&x);
        while(x--)
        {
            int t;
            scanf("%d",&t);
            printf("%d\n",cnt[t]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/81876846