ZOJ2849 优先队列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 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.....

  1 #include <stdio.h>
  2 
  3 struct node{
  4     char flag;//'1' indicates be in virus
  5     int type;
  6     int level;
  7 }num[501][501];
  8 
  9 int main(){
 10     int day ,row ,column ;
 11     int i,j,k,flag,num_t,num_flagz;
 12     int sum_Target,target_Type,target_Num;
 13 
 14     while(scanf("%d%d",&row,&column)!=EOF){
 15         num_flagz=0;
 16         for(i=1;i<=row;i++){
 17             for(j=1;j<=column;j++){
 18                 scanf("%d",&num_t);
 19                 if(num_t > 0){
 20                     num[i][j].type = num_t;
 21                     num[i][j].flag = '1';
 22                 }else if(num_t < 0){
 23                         num[i][j].level = -num_t;
 24                         num[i][j].flag = '0';
 25                         num_flagz++;
 26                     }
 27             }
 28         }
 29 
 30         day = 1;
 31         while(num_flagz != 0){
 32             int times = row * column;
 33             while(times--){
 34                 for(i=1;i<=row;i++){
 35                     for(j=1;j<=column;j++){
 36                         if(num[i][j].flag == '1' || num[i][j].flag == '-1'){
 37                             if(i-1>=1 && i-1<=row && j>=1 && j<=column){
 38                                 if(num[i-1][j].flag == '0' && num[i-1][j].level <= day){
 39                                     num[i-1][j].flag = '-1';//wait to change
 40                                     num[i-1][j].type = num[i][j].type;
 41                                 }
 42                                 if(num[i-1][j].flag == '-1'){
 43                                     if(num[i][j].type < num[i-1][j].type)
 44                                         num[i-1][j].type = num[i][j].type;
 45                                 }
 46                             }
 47                             if(i>=1 && i<=row && j-1>=1 && j-1<=column){
 48                                 if(num[i][j-1].flag == '0' && num[i][j-1].level <= day){
 49                                     num[i][j-1].flag = '-1';//wait to change
 50                                     num[i][j-1].type = num[i][j].type;
 51                                 }
 52                                 if(num[i][j-1].flag == '-1'){
 53                                     if(num[i][j].type < num[i][j-1].type)
 54                                         num[i][j-1].type = num[i][j].type;
 55                                 }
 56                             }
 57                             if(i>=1 && i<=row && j+1>=1 && j+1<=column){
 58                                 if(num[i][j+1].flag == '0' && num[i][j+1].level <= day){
 59                                     num[i][j+1].flag = '-1';//wait to change
 60                                     num[i][j+1].type = num[i][j].type;
 61                                 }
 62                                 if(num[i][j+1].flag == '-1'){
 63                                     if(num[i][j].type < num[i][j+1].type)
 64                                         num[i][j+1].type = num[i][j].type;
 65                                 }
 66                             }
 67                             if(i+1>=1 && i+1<=row && j>=1 && j<=column){
 68                                 if(num[i+1][j].flag == '0' && num[i+1][j].level <= day){
 69                                     num[i+1][j].flag = '-1';//wait to change
 70                                     num[i+1][j].type = num[i][j].type;
 71                                 }
 72                                 if(num[i+1][j].flag == '-1'){
 73                                     if(num[i][j].type < num[i+1][j].type)
 74                                         num[i+1][j].type = num[i][j].type;
 75                                 }
 76                             }
 77                         }
 78                     }
 79                 }
 80             }
 81 
 82             num_flagz = 0;
 83             for(i=1;i<=row;i++){
 84                 for(j=1;j<=column;j++){
 85                     if(num[i][j].flag == '0')
 86                         num_flagz++;
 87                 }
 88             }
 89 
 90             /*
 91             printf("day %d:
",day);
 92             for(i=1;i<=row;i++){
 93                 for(j=1;j<=column;j++){
 94                     if(num[i][j].flag == '1' || num[i][j].flag =='-1')
 95                         printf("%d ",num[i][j].type);
 96                     else if(num[i][j].flag == '0')
 97                         printf("0 ");
 98                 }
 99                 printf("
");
100             }
101             */
102             day++;
103         }
104 
105         scanf("%d",&sum_Target);
106         for(int t=0;t<sum_Target;t++){
107             scanf("%d",&target_Type);
108             target_Num = 0;
109             for(i=1;i<=row;i++){
110                 for(j=1;j<=column;j++){
111                     if(num[i][j].flag == '-1' || num[i][j].flag == '1'){
112                         if(num[i][j].type == target_Type)
113                             target_Num++;
114                     }
115                 }
116             }
117             printf("%d
",target_Num);
118         }
119     }
120     return 0;
121 }
View Code

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

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

2、类型小的优先感染

3、只能感染相邻的

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

 1 [cpp] view plaincopyprint?
 2 #include<stdio.h>  
 3 #include<string.h>  
 4 #include<queue>  
 5 #define INF 1<<30  
 6 using namespace std;  
 7 struct node{  
 8     int day;  
 9     int type;  
10     int x;  
11     int y;  
12     friend bool operator <(node a,node b)  
13     {  
14         if(a.day!=b.day)  
15             return a.day>b.day;  
16         else  
17             return a.type>b.type;  
18     }  
19 };  
20 priority_queue<node> q;  
21 int m,n;  
22 int map[555][555];  
23 //int sum[250010];  
24 int sum[250010];  
25 int move[4][2]={{1,0},{-1,0},{0,1},{0,-1}};  
26 void bfs()  
27 {  
28     int i,j,k;  
29     while(!q.empty())  
30     {  
31         k=0;  
32         node p=q.top();  
33         node t;  
34         q.pop();  
35         k=-INF;  
36         for(i=0;i<4;i++)  
37         {  
38             t.x=p.x+move[i][0];  
39             t.y=p.y+move[i][1];  
40             if(t.x>0&&t.x<=m&&t.y>0&&t.y<=n&&map[t.x][t.y]<0)  
41             {  
42                 if(map[t.x][t.y]+p.day>=0)//可以被感染  
43                 {  
44                     t.type=p.type;  
45                     t.day=p.day;  
46                     q.push(t);  
47                     sum[t.type]++;  
48                     map[t.x][t.y]=t.type;  
49                 }  
50                 else    
51                 {  
52                     if(map[t.x][t.y]>k)  
53                         k=map[t.x][t.y];  
54                 }  
55             }  
56         }  
57         if(k!=-INF)  
58         {  
59             p.day=k*(-1);  
60             q.push(p);  
61         }  
62     }  
63 }  
64 int main()  
65 {  
66     int i,j,k,l;  
67     while(~scanf("%d %d",&m,&n))  
68     {  
69         memset(sum,0,sizeof(sum));  
70         memset(map,0,sizeof(map));  
71         for(i=1;i<=m;i++)  
72         {  
73             for(j=1;j<=n;j++)  
74             {  
75                 scanf("%d",&map[i][j]);  
76                 if(map[i][j]>0)  
77                 {  
78                     node p;  
79                     p.day=1;  
80                     p.type=map[i][j];  
81                     p.x=i;  
82                     p.y=j;  
83                     sum[p.type]++;  
84                     q.push(p);  
85                 }  
86             }  
87         }  
88         bfs();  
89         scanf("%d",&k);  
90         for(i=0;i<k;i++)  
91         {  
92             int t;  
93             scanf("%d",&t);  
94             printf("%d
",sum[t]);  
95         }  
96     }  
97     return 0;  
98 }  

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

 1 #include <stdio.h>  
 2 #include <string.h>  
 3 #include <queue>  
 4 using namespace std;  
 5 const int MAX = 510;  
 6 struct node  
 7 {  
 8     int day;  
 9     int type;  
10     int x;  
11     int y;  
12     bool friend operator < (node a,node b)  
13     {  
14         if(a.day != b.day)  
15             return a.day > b.day;  
16         return a.type > b.type;  
17     }  
18 };  
19 priority_queue<node> q;  
20 int n,m;  
21 int cnt[MAX*MAX];  
22 int a[MAX][MAX];  
23 int dir[4][2] = {0,1,0,-1,1,0,-1,0};  
24    
25 void bfs()  
26 {  
27     int i;  
28     while(!q.empty())  
29     {  
30         int flag = 0;  
31         node p = q.top();  
32         q.pop();  
33         for(i = 0;i < 4; i++)  
34         {  
35             node t;  
36             t.x = p.x + dir[i][0];  
37             t.y = p.y + dir[i][1];  
38             if(t.x >= 1 && t.x <= n && t.y >= 1 && t.y <= m && a[t.x][t.y] < 0)  
39             {  
40                 if(p.day >= a[t.x][t.y] * (-1))  
41                 {  
42                     t.type = p.type;  
43                     t.day = p.day;  
44                     a[t.x][t.y] = p.type;  
45                     q.push(t);  
46                     cnt[p.type]++;  
47                 }  
48                 else  
49                 {  
50                     if(a[t.x][t.y] > flag || !flag)  
51                         flag = a[t.x][t.y];  
52                 }  
53             }  
54         }  
55         if(flag)  
56         {  
57             p.day = -flag;  
58             q.push(p);  
59         }  
60     }  
61 }  
62 int main()  
63 {  
64     int i,j,k,t;  
65     node x;  
66     while(scanf("%d %d",&n,&m)!=EOF)  
67     {  
68         while(!q.empty())  
69             q.pop();  
70         memset(cnt,0,sizeof(cnt));  
71         for(i = 1;i <= n; i++)  
72         {  
73             for(j = 1;j <= m; j++)  
74             {  
75                 scanf("%d",&a[i][j]);  
76                 if(a[i][j] > 0)  
77                 {  
78                     x.x = i;  
79                     x.y = j;  
80                     x.type = a[i][j];  
81                     x.day = 1;  
82                     cnt[a[i][j]]++;  
83                     q.push(x);  
84                 }  
85             }  
86         }  
87         bfs();  
88         scanf("%d",&k);  
89         while(k--)  
90         {  
91             scanf("%d",&t);  
92             printf("%d
",cnt[t]);  
93         }  
94     }  
95     return 0;  
96 }  
原文地址:https://www.cnblogs.com/wushuaiyi/p/3606463.html