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. python自定义排序函数

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  2. [转]IE和Firefox兼容性问题及解决方法

    今天测试代码时,发现不少IE可以运行的ajax,但在FF中报错.IE和Firefox(火狐)在JavaScript方面的不兼容及统一方法总结如下: 1.兼容firefox的 outerHTML,FF中 ...

  3. 2014第35周三jquery最近用到的内容总结

    1.文档加载后执行: $(document).ready(function(){//onload();}); 或$(function(){//onload();}) 2. 选择器使用: $(" ...

  4. android LinearLayout和RelativeLayout实现精确布局

    先明确几个概念的区别: padding margin:都是边距的含义,关键问题得明白是什么相对什么的边距padding:是控件的内容相对控件的边缘的边距. margin  :是控件边缘相对父空间的边距 ...

  5. 关于CDC在非控件类中的使用

    在非CStatic的派生类中,由于进行图形的绘制的话,我们需要对该类传入一个CDC以便于绘画.这是因为非CStatic等控件类无法自己产生onPaint这类的消息,因此需要借传入的CDC进行回执,然后 ...

  6. phpmyadmin出现空password登录被禁止

    在Windows或者Linux下mysql安装后默认的password为空,又当我们又安装了mysql的管理工具 phpmyadmin后登陆时出现"空password登陆呗禁止(參见同意pa ...

  7. GDB命令行最基本操作

    程序启动: A.冷启动 gdb program              e.g., gdb ./cs gdb –p pid                 e.g., gdb –p `pidof c ...

  8. LINQ 图解

    LINQ 图解 原创地址:http://www.cnblogs.com/jfzhu/archive/2013/01/01/2841332.html 转载请注明出处 LINQ,语言集成查询(Langua ...

  9. Python之路Day15

    主要内容:WEB框架.Django基础 WEB框架 Web请求流程 -- 原始Web框架 -- 自定义Web框架 -- MVC 和 MTV # Models Views Controllers # 模 ...

  10. [LeetCode]题解(python):016-3Sum Closest

    题目来源: https://leetcode.com/problems/3sum-closest/ 题意分析: 这道题目输入一个数组nums和一个数target,找出数组中三个数,使得他们的和最接近t ...