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:
- The virus can only spread along the network from the already infected computers to the clean ones.
- If a computer has already been infected by one virus variation, it will never be infected by another variation.
- 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.
- 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 <= M, N <= 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的更多相关文章
- 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 ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- 【UESTC 482】Charitable Exchange(优先队列+bfs)
给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...
- cdoj 482 优先队列+bfs
Charitable Exchange Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
随机推荐
- 0603 python 基础02
作业1:ANSI和utf8的区别? ASCII是用来表示英文字符的一种编码规范,每个ASCII字符占用1个字节(8bits). 可以表示的最大字符数是256,一般只用前128个(最高位为0),其中包括 ...
- 正则语法笔记-regular expression note
参考文档:python正则表达式 正则表达式定义:正则是一门高度专业编程语言,内嵌在其他语言(python re模块)中使用.正则表达式包含元字符(metacharacter)列表,列表如下: . ^ ...
- React使用笔记1-React的JSX和Style
React使用笔记1-React的JSX和Style Date: 2015-11-27 20:56 Category: Web Tags: JavaScript Author: 刘理想 [toc] 1 ...
- 前端笔试题 JS部分
题目 http://www.itmian4.com/forum.php?mod=viewthread&tid=4540 http://www.itmian4.com/forum.php?mod ...
- 试用阿里云RDS的MySQL压缩存储引擎TokuDB
以前就用过自己搭建MySQL服务器的两种存储引擎MyISAM和InnoDB(也用过一点Memory方式),在今年初转向阿里云关系型数据库服务RDS的时候,看到可调参数中有一个TokuDB,不过不太了解 ...
- 求最大值最小值的方法 时间复杂度O(n)
#include<iostream> #include <iostream> #include <bitset> #include <ctime> us ...
- 链队列之C++实现
链队列时建立在单链表的基础之上的.由于是动态分配节点内存,所以无需判满. 链队列的形式如下: 1.队列空 2.队列存在数据 下面介绍下C++实现的链队列,VC6下调试通过. 1.文件组织 2.lq.h ...
- jvm学习小结
1. JDK.JRE.JVM之间的关系.JDK包含JRE和其它开发工具库如编译器.调试期,jConsele性能检测工具等2. JVM的构成:类装载器子系统.执行引擎.运行时数据区,如下图: 3. JV ...
- Android 全屏方法
我大概不想赘述什么其他方法,我就说一下我已知在用的方法QAQ requestWindowFeature(Window.FEATURE_NO_TITLE); 设置程序无标题栏 getWindow().s ...
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...