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

暴利果然超时了= = sigh.....

 #include <stdio.h>

 struct node{
char flag;//'1' indicates be in virus
int type;
int level;
}num[][]; int main(){
int day ,row ,column ;
int i,j,k,flag,num_t,num_flagz;
int sum_Target,target_Type,target_Num; while(scanf("%d%d",&row,&column)!=EOF){
num_flagz=;
for(i=;i<=row;i++){
for(j=;j<=column;j++){
scanf("%d",&num_t);
if(num_t > ){
num[i][j].type = num_t;
num[i][j].flag = '';
}else if(num_t < ){
num[i][j].level = -num_t;
num[i][j].flag = '';
num_flagz++;
}
}
} day = ;
while(num_flagz != ){
int times = row * column;
while(times--){
for(i=;i<=row;i++){
for(j=;j<=column;j++){
if(num[i][j].flag == '' || num[i][j].flag == '-1'){
if(i->= && i-<=row && j>= && j<=column){
if(num[i-][j].flag == '' && num[i-][j].level <= day){
num[i-][j].flag = '-1';//wait to change
num[i-][j].type = num[i][j].type;
}
if(num[i-][j].flag == '-1'){
if(num[i][j].type < num[i-][j].type)
num[i-][j].type = num[i][j].type;
}
}
if(i>= && i<=row && j->= && j-<=column){
if(num[i][j-].flag == '' && num[i][j-].level <= day){
num[i][j-].flag = '-1';//wait to change
num[i][j-].type = num[i][j].type;
}
if(num[i][j-].flag == '-1'){
if(num[i][j].type < num[i][j-].type)
num[i][j-].type = num[i][j].type;
}
}
if(i>= && i<=row && j+>= && j+<=column){
if(num[i][j+].flag == '' && num[i][j+].level <= day){
num[i][j+].flag = '-1';//wait to change
num[i][j+].type = num[i][j].type;
}
if(num[i][j+].flag == '-1'){
if(num[i][j].type < num[i][j+].type)
num[i][j+].type = num[i][j].type;
}
}
if(i+>= && i+<=row && j>= && j<=column){
if(num[i+][j].flag == '' && num[i+][j].level <= day){
num[i+][j].flag = '-1';//wait to change
num[i+][j].type = num[i][j].type;
}
if(num[i+][j].flag == '-1'){
if(num[i][j].type < num[i+][j].type)
num[i+][j].type = num[i][j].type;
}
}
}
}
}
} num_flagz = ;
for(i=;i<=row;i++){
for(j=;j<=column;j++){
if(num[i][j].flag == '')
num_flagz++;
}
} /*
printf("day %d:\n",day);
for(i=1;i<=row;i++){
for(j=1;j<=column;j++){
if(num[i][j].flag == '1' || num[i][j].flag =='-1')
printf("%d ",num[i][j].type);
else if(num[i][j].flag == '0')
printf("0 ");
}
printf("\n");
}
*/
day++;
} scanf("%d",&sum_Target);
for(int t=;t<sum_Target;t++){
scanf("%d",&target_Type);
target_Num = ;
for(i=;i<=row;i++){
for(j=;j<=column;j++){
if(num[i][j].flag == '-1' || num[i][j].flag == ''){
if(num[i][j].type == target_Type)
target_Num++;
}
}
}
printf("%d\n",target_Num);
}
}
return ;
}

附加结题报告from:http://blog.csdn.net/yan_____/article/details/8656731

1、被感染的机器防御等级<=天数

2、类型小的优先感染

3、只能感染相邻的

4、一天之内能感染的全部都可以感染完

 [cpp] view plaincopyprint?
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 1<<30
using namespace std;
struct node{
int day;
int type;
int x;
int y;
friend bool operator <(node a,node b)
{
if(a.day!=b.day)
return a.day>b.day;
else
return a.type>b.type;
}
};
priority_queue<node> q;
int m,n;
int map[][];
//int sum[250010];
int sum[];
int move[][]={{,},{-,},{,},{,-}};
void bfs()
{
int i,j,k;
while(!q.empty())
{
k=;
node p=q.top();
node t;
q.pop();
k=-INF;
for(i=;i<;i++)
{
t.x=p.x+move[i][];
t.y=p.y+move[i][];
if(t.x>&&t.x<=m&&t.y>&&t.y<=n&&map[t.x][t.y]<)
{
if(map[t.x][t.y]+p.day>=)//可以被感染
{
t.type=p.type;
t.day=p.day;
q.push(t);
sum[t.type]++;
map[t.x][t.y]=t.type;
}
else
{
if(map[t.x][t.y]>k)
k=map[t.x][t.y];
}
}
}
if(k!=-INF)
{
p.day=k*(-);
q.push(p);
}
}
}
int main()
{
int i,j,k,l;
while(~scanf("%d %d",&m,&n))
{
memset(sum,,sizeof(sum));
memset(map,,sizeof(map));
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]>)
{
node p;
p.day=;
p.type=map[i][j];
p.x=i;
p.y=j;
sum[p.type]++;
q.push(p);
}
}
}
bfs();
scanf("%d",&k);
for(i=;i<k;i++)
{
int t;
scanf("%d",&t);
printf("%d\n",sum[t]);
}
}
return ;
}

or from http://www.2cto.com/kf/201311/257413.html

 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int MAX = ;
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[][] = {,,,-,,,-,}; void bfs()
{
int i;
while(!q.empty())
{
int flag = ;
node p = q.top();
q.pop();
for(i = ;i < ; i++)
{
node t;
t.x = p.x + dir[i][];
t.y = p.y + dir[i][];
if(t.x >= && t.x <= n && t.y >= && t.y <= m && a[t.x][t.y] < )
{
if(p.day >= a[t.x][t.y] * (-))
{
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,,sizeof(cnt));
for(i = ;i <= n; i++)
{
for(j = ;j <= m; j++)
{
scanf("%d",&a[i][j]);
if(a[i][j] > )
{
x.x = i;
x.y = j;
x.type = a[i][j];
x.day = ;
cnt[a[i][j]]++;
q.push(x);
}
}
}
bfs();
scanf("%d",&k);
while(k--)
{
scanf("%d",&t);
printf("%d\n",cnt[t]);
}
}
return ;
}

ZOJ2849 优先队列BFS的更多相关文章

  1. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  4. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  5. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  6. 【UESTC 482】Charitable Exchange(优先队列+bfs)

    给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...

  7. cdoj 482 优先队列+bfs

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  8. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  9. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

随机推荐

  1. 简明的例子讲解position:relative、float、overflow:hidden和inline-block

    标签(空格分隔): css relative float 我们通过一个简单的实验来了解position:relative float overflow:hidden 和 inline-block. 下 ...

  2. LintCode- 删除排序数组中的重复数字

    题目描述: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 样例 给出数组A =[1,1 ...

  3. css为网页顶部和底部都加入背景图

    网页背景图是我们常用的功能,一般来说.给网页加一个背景图,只要在网页的body标签中加入css属性就行. 代码如下:<body style="background-image:url( ...

  4. HDU 1398 Square Coins

    题目大意:有面值分别为.1,4,9,.......17^2的硬币无数多个.问你组成面值为n的钱的方法数. 最简单的母函数模板题: #include <cstdio> #include &l ...

  5. 获取中央气象台API 完整城市列表简单方式

    activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  6. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  7. Java集合框架的知识总结

    说明:面试准备,写的挺不错的. 转载地址: http://www.cnblogs.com/zhxxcq/archive/2012/03/11/2389611.html 1.综述 所有集合类都位于jav ...

  8. float存储方式编程验证

    取出float在内存中的编码: void printFloatAsBinary(float f){ // 二进制的位数 const int bits = sizeof(f) * 8; // 将floa ...

  9. SQL 语言划分

    从功能上划分,SQL 语言能够分为DDL,DML和DCL三大类. 1. DDL(Data Definition Language)     数据定义语言,用于定义和管理 SQL 数据库中的全部对象的语 ...

  10. poj2486 Apple Tree【区间dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4374766.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...