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 re(正则模块)

    参考文档:http://blog.csdn.net/wusuopubupt/article/details/29379367 ipython环境中,输入"?re",官方解释如下: ...

  2. 帝国cms7.0 列表模板调用本栏目缩略图

    [e:loop={"select classimg from phome_enewsclass where classid='$GLOBALS[navclassid]'",1,24 ...

  3. javascript的isPrototypeOf函数的理解

    JavaScript中isPrototypeOf函数方法是返回一个布尔值,指出对象是否存在于另一个对象的原型链中.使用方法: object1.isPrototypeOf(object2)~~~原型链理 ...

  4. zookeeper 同步

    <pre name="code" class="html">一个节点上的数据发生变化后,通知其他节点 server 1: [root@wx03 bi ...

  5. Android 通过Dom, Sax, Pull解析网络xml数据

    这篇文章不是完全原创,XML解析的部分参考了 liuhe688 的文章.文章地址:http://blog.csdn.net/liuhe688/article/details/6415593 这是一个几 ...

  6. Android 4.4及以上系统下应用的状态栏颜色渐变效果的实现

    上一篇转载的博文里讲到了怎么开启状态栏透明的效果,不过如果在有ActionBar的情况下,会出现状态栏透明而ActionBar横亘在状态栏和内容之间的丑陋情况,如下图: 通过百度之后,发现了GitHu ...

  7. Parsing HTML with C++ (using Qt preferably) - Stack Overflow

    Parsing HTML with C++ (using Qt preferably) - Stack Overflow Parsing HTML with C++ (using Qt prefera ...

  8. uestc 250 数位dp(水)

    /* 数位dp 水题 开两维一个记录长度,一个记录上一个数 */ #include<stdio.h> #include<string.h> #define N 13 int d ...

  9. ORACLE分科目统计每科前三名的学生的语句

    有个成绩表 score(student_no,Subject_no,Score)分别为学号,课程号,成绩.我想用语句查询出每科的前三名学生的学号,请各位高手教教小弟 1.创建测试语句:create t ...

  10. 自定义cell相关注意事项

    1.拖线成功后,如果又在.h文件或者.m文件里面删除了对应的属性或者方法.一定要在xib文件中,删除关联.方法是:右键点击一下对应的UI控件,把多余的关联叉掉就行了.  不然容易崩溃.