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经过,怪物需要 ...
随机推荐
- <转> Python的优雅技巧
枚举 不要这么做: 全选复制放进笔记 i = 0 for item in iterable: print i, item i += 1 而是这样: 全选复制放进笔记 for i, item in en ...
- MVC-03 控制器(3)
Controller负责处理浏览器来的所有要求,并决定响应什么属性给浏览器,以及协调Model与View之间的数据传递.在ASP.NET MVC中有好几种传递数据给视图的方式,例如从ASP.NET M ...
- 设置Firefox禁用js缓存
让firefox禁用缓存 让Firefox不再使用缓存网站开发时经常会有这样的疑问:为什么修改了代码,刷新了页面还是没有看到改动呢? 其实,可能只是你的Firefox并没有去下载你更新了的文件. 这时 ...
- QT 自动获取可用串口
本来想直接用Settings来获取的,但是串口信息类似 "\Device\Serial0",死活获取不了,用了转义.反斜杠还是获取不到,所以就放弃了,网上好像也没有获取成功的. 所 ...
- perl5 第九章 关联数组/哈希表
第九章 关联数组/哈希表 by flamephoenix 一.数组变量的限制二.定义三.访问关联数组的元素四.增加元素五.创建关联数组六.从数组变量复制到关联数组七.元素的增删八.列出数组的索引和值九 ...
- mrtg监控网络流量简单配置
Mrtg服务器搭建(监控网络流量) [日期:2012-07-03] 来源:Linux社区 作者:split_two [字体:大 中 小] [实验环境] 监控机:Red Hat linux 5.3 ...
- File,FileInputStream,FileReader,InputStreamReader,BufferedReader 的使用和区别
1 ) File 类介绍 File 类封装了对用户机器的文件系统进行操作的功能.例如,可以用 File 类获得文件上次修改的时间移动, 或者对文件进行删除.重命名.换句话说,流类关注的是文件内容,而 ...
- p2.js物理引擎学习
P2简介 P2是一款基于Javascript编写的HTML5 2D物理引擎,和Box2D.Nape等2D物理引擎一样,P2集成了各种复杂的物理公式和算法,可以帮助我们轻松的实现碰撞.反弹等物理现象的模 ...
- UIPickView之自定义生日键盘和城市键盘
//// ViewController.m// 04-键盘处理// // #import "ViewController.h"#import "XMGProvince ...
- Leetcode 动态规划 Unique Paths
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths Total Accepted: 17915 Total Submi ...