zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事。。。
不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~
求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit time 。SO,杀死一个护卫到达那个格子 2units time。
第一反应是广搜,就搜咧 = =。。
WA了,交hdu上 AC了,hdu数据真弱啊。。。
想了想,想通了,因为一般广搜的话必须都是1个时间才能搜,才能保证这个BFS树是等距离向外伸展的,而这个不是等距离的,所以需要一些处理。
1、我的方法是,找到天使后,把时间比下大小,最后输出最小的。需要优化,只这么做的话,会TLE的,如果走过一个格子,这个格子存走过时候的时间,下次再走到这个格子,如果时间比格子里的短,就入队,否则,就不用入队了。60MS。
2、网上看到另一种方法,就是把杀护卫和走到护卫那个格子看成两个动作等于说是入队两次,这个好啊!!!写了半天终于写出来了。20MS。
3、刚才想起来一种方法,因为如果等距离的BFS的话,队列里的time值是从小往大排的,那我直接用优先队列就可以了哈~~嘻嘻~10MS~人品好,爆0MS了~~
法I
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <queue>
#include <limits.h>
#define MAX 205
using namespace std;
typedef struct ANG{
int t;
int x,y;
}ANG;
queue <ANG> q;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m,mmin;
int dir[] = {,,,-,,,-,};
void BFS()
{
ANG tmp;
int i,x,a,b,aa,bb,t;
while( !q.empty() )
{
tmp = q.front();
q.pop();
a = tmp.x;
b = tmp.y;
t = tmp.t;
for(i=; i<; i+=)
{
aa = a + dir[i];
bb = b + dir[i+];
if( aa >= && aa < n && bb >= && b < m && map[aa][bb] != '#' && vis[aa][bb] )
{
if( map[aa][bb] == 'a' )
{
if( t+ < mmin )
mmin = t+;
continue;
} if( map[aa][bb] == '.' && t+ < vis[aa][bb] ) //如果走过,而且时间比现在的短,就没必要入队了。
{
vis[aa][bb] = t+;
tmp.t = t + ;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
}
if( map[aa][bb] == 'x' && t+ < vis[aa][bb] )
{
vis[aa][bb] = t+;
tmp.t = t + ;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
}
}
}
}
}
int main()
{
int i,k,ans;
ANG tmp;
while( ~scanf("%d%d",&n,&m) )
{
memset(map,,sizeof(map));
for(i=; i<n; i++)
scanf("%s",map[i]); while( !q.empty() )
q.pop(); for(i=; i<n; i++)
for(k=; k<m; k++)
vis[i][k] = INT_MAX; for(i=; i<n; i++)
for(k=; k<m; k++)
if( map[i][k] == 'r' )
{
map[i][k] = '.';
tmp.x = i;
tmp.y = k;
tmp.t = ;
q.push(tmp);
vis[i][k] = ;
}
mmin = INT_MAX;
BFS();
if( mmin != INT_MAX )
printf("%d/n",mmin);
else
printf("Poor ANGEL has to stay in the prison all his life./n");
}
return ;
}
法II
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <queue>
#include <limits.h>
#define MAX 201
using namespace std;
typedef struct ANG{
int t;
int x,y,flag;
}ANG;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int dir[] = {,,,-,,,-,};
priority_queue<ANG> q;
bool operator<(ANG a, ANG b)
{
return a.t > b.t;
}
int BFS()
{
ANG tmp;
int i,x,a,b,aa,bb,t;
while( !q.empty() )
{
tmp = q.top();
q.pop();
a = tmp.x;
b = tmp.y;
t = tmp.t;
if( map[a][b] == 'x' && tmp.flag == )
{
tmp.x = a;
tmp.y = b;
tmp.t = t+;
tmp.flag = ;
q.push(tmp);
vis[a][b] = ;
continue;
}
for(i=; i<; i+=)
{
aa = a + dir[i];
bb = b + dir[i+];
if( aa >= && aa < n && bb >= && b < m && map[aa][bb] != '#' && !vis[aa][bb] )
{
if( map[aa][bb] == 'a' )
return t+; if( map[aa][bb] == '.' )
{
vis[aa][bb] = ;
tmp.t = t + ;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
} if( map[aa][bb] == 'x' )
{
vis[aa][bb] = ;
tmp.flag = ;
tmp.t = t + ;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
}
}
}
}
return -;
}
int main()
{
int i,k,ans;
ANG tmp;
while( ~scanf("%d%d",&n,&m) )
{
memset(map,,sizeof(map));
for(i=; i<n; i++)
scanf("%s",map[i]); while( !q.empty() )
q.pop();
memset(vis,,sizeof(vis)); for(i=; i<n; i++)
for(k=; k<m; k++)
if( map[i][k] == 'r' )
{
map[i][k] = '.';
tmp.x = i;
tmp.y = k;
tmp.t = ;
q.push(tmp);
vis[i][k] = ;
}
ans = BFS();
if( ans != - )
printf("%d/n",ans);
else
printf("Poor ANGEL has to stay in the prison all his life./n");
}
return ;
}
法III
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <queue>
#include <limits.h>
#define MAX 201
using namespace std;
typedef struct ANG{
int t;
int x,y;
}ANG;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int dir[] = {,,,-,,,-,};
priority_queue<ANG> q;
bool operator<(ANG a, ANG b)
{
return a.t > b.t;
}
int BFS()
{
ANG tmp;
int i,x,a,b,aa,bb,t;
while( !q.empty() )
{
tmp = q.top();
q.pop();
a = tmp.x;
b = tmp.y;
t = tmp.t;
for(i=; i<; i+=)
{
aa = a + dir[i];
bb = b + dir[i+];
if( aa >= && aa < n && bb >= && b < m && map[aa][bb] != '#' && !vis[aa][bb] )
{
vis[aa][bb] = ;
if( map[aa][bb] == 'a' )
return t+; if( map[aa][bb] == '.' )
{
tmp.t = t + ;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
}
if( map[aa][bb] == 'x' )
{
tmp.t = t + ;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
}
}
}
}
return -;
}
int main()
{
int i,k,ans;
ANG tmp;
while( ~scanf("%d%d",&n,&m) )
{
memset(map,,sizeof(map));
for(i=; i<n; i++)
scanf("%s",map[i]); while( !q.empty() )
q.pop();
memset(vis,,sizeof(vis)); for(i=; i<n; i++)
for(k=; k<m; k++)
if( map[i][k] == 'r' )
{
map[i][k] = '.';
tmp.x = i;
tmp.y = k;
tmp.t = ;
q.push(tmp);
vis[i][k] = ;
}
ans = BFS();
if( ans != - )
printf("%d/n",ans);
else
printf("Poor ANGEL has to stay in the prison all his life./n");
}
return ;
}
zoj 1649 Rescue (BFS)(转载)的更多相关文章
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- zoj 1649 Rescue
BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...
- BFS zoj 1649
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- zoj 1649 bfs
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...
随机推荐
- Kafka系列(一)安装和配置说明
单机模式 修改kafak安装文件中 .../kafka_2.9.2-0.8.1.1/config 下面的server.properties 配置文件 1.broker.id=0 [默认不用修改,该 ...
- HDOJ-ACM1023(JAVA)
题意:输入栈的大小,输出可能的出栈顺序的个数. 这道题,如果做了1022,那就只要在上面改改就行了, 第一想法是加上全排列-----结果是正确的,但是绝对会超时 验证性的实现了:(Time Limit ...
- 最新Blog
应该会长期稳定的 https://b.intmainreturn0.com/
- HDU5407.CRB and Candies(数论)
官方题解: The problem is just to calculate g(N) = LCM(C(N,0),C(N,1),...,C(N,N)) Introducing function f(n ...
- 让SecureCRT vi中显示多色彩
方法1: 1.打开SecureCRT___选项__会话选项___仿真 右边的中端选择linux,把ANSI 颜色和使用颜色方案打上钩. 2.选择外观,右边的使用颜色和闪烁都打上钩. 3.重新连接使所该 ...
- [置顶] 使用U盘安装ubuntu系统
使用U盘安装ubuntu系统 在网上找了很多教程,都不起效,提示:“从光盘上读取数据出错”. 总结出了几个关键点. 首先,版本,Ubuntu 12.04 Server,一般的U盘安装都会报:“从光盘上 ...
- [HAOI2012] 容易题
有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪些值,我们定义一个数列的积为该数列所有元素的乘积,要求你求出所有可能的数列的积的和 mod 1000000007的 ...
- EasyUI Datagrid 自定义列、Foolter及单元格编辑
1:自定义列,包括 Group var head1Array = []; head1Array.push({ field: 'Id', title: 'xxxx', rowspan: 2 }); he ...
- google perftools分析程序性能
Google perftools 1.功能简介 它的主要功能就是通过采样的方式,给程序中cpu的使用情况进行“画像”,通过它所输出的结果,我们可以对程序中各个函数(得到函数之间的调用关系)耗时情况一目 ...
- RHCA442学习笔记-Unit10内存地址及分配
Unit 10 Memory Addressing and Allocation 内存地址及分配 学习目标: A. 虚拟地址与物理地 B. 调整内存地址分配 C. 解析内存溢出 10.1 O ...