TOJ 2690 ZOJ 2849 Attack of Panda Virus / 优先队列+广搜

Attack of Panda Virus

时间限制(普通/Java):3000MS/30000MS     运行内存限制:65536KByte

描述

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 ofM rows andN 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... TypeT). Each computer in the network has a specific defense levelL (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.

输入

The input contains multiple test cases!

On the first line of each test case are two integersM andN (1 <=M, N <= 500), followed by a M * N matrix. A positive integerT in the matrix indicates that the corresponding computer had already been infected by the virus variations of typeT at the beginning while a negative integer-L indicates that the computer has a defense levelL. Then there is an integerQ indicating the number of queries. Each of the followingQ lines has an integer which is the virus variation type we care.

输出

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.

样例输入

3 4
1 -3 -2 -3
-2 -1 -2 2
-3 -2 -1 -1
2
1
2

样例输出

9
3
day不一样的day小的在前 一样的话type小的在前 如果当前点的周围还有没被传染的电脑 你们当前点的day更新到周围level最小的 在进队列
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int MAX = 510;
struct node
{
	int day;
	int type;
	int x;
	int y;
	bool friend operator < (node a,node b)
	{
		if(a.day != b.day)
			return a.day > b.day;
		return a.type > b.type;
	}
};
priority_queue<node> q;
int n,m;
int cnt[MAX*MAX];
int a[MAX][MAX];
int dir[4][2] = {0,1,0,-1,1,0,-1,0};

void bfs()
{
	int i;
	while(!q.empty())
	{
		int flag = 0;
		node p = q.top();
		q.pop();
		for(i = 0;i < 4; i++)
		{
			node t;
			t.x = p.x + dir[i][0];
			t.y = p.y + dir[i][1];
			if(t.x >= 1 && t.x <= n && t.y >= 1 && t.y <= m && a[t.x][t.y] < 0)
			{
				if(p.day >= a[t.x][t.y] * (-1))
				{
					t.type = p.type;
					t.day = p.day;
					a[t.x][t.y] = p.type;
					q.push(t);
					cnt[p.type]++;
				}
				else
				{
					if(a[t.x][t.y] > flag || !flag)
						flag = a[t.x][t.y];
				}
			}
		}
		if(flag)
		{
			p.day = -flag;
			q.push(p);
		}
	}
}
int main()
{
	int i,j,k,t;
	node x;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		while(!q.empty())
			q.pop();
		memset(cnt,0,sizeof(cnt));
		for(i = 1;i <= n; i++)
		{
			for(j = 1;j <= m; j++)
			{
				scanf("%d",&a[i][j]);
				if(a[i][j] > 0)
				{
					x.x = i;
					x.y = j;
					x.type = a[i][j];
					x.day = 1;
					cnt[a[i][j]]++;
					q.push(x);
				}
			}
		}
		bfs();
		scanf("%d",&k);
		while(k--)
		{
			scanf("%d",&t);
			printf("%d\n",cnt[t]);
		}
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值