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. ...
随机推荐
- javascript设计模式2
接口:利 固化一部分代码 弊 丧失js的灵活性 在JavaScript中模仿接口 /* interface Composite{ function add(child); function remov ...
- HDOJ-ACM1003(JAVA)
转载声明:原文转自http://www.cnblogs.com/xiezie/p/5502855.html 第一.二次的思路都是穷举: 第一次的实现是用二维数组: import java.util.* ...
- hdoj 3572 Task Schedule【建立超级源点超级汇点】
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 关于sqlite数据库
firedac数据引擎可以支持sqlite数据库,这种方式是纯绿色的,发布时不需要带上官方的sqlite.dll动态链接库文件. 当然调用该动态链接库的API方法也是可以操作sqlite数据库的,这样 ...
- 10670 Work Reduction (贪心 + 被题意坑了- -)y
Problem C: Work Reduction Paperwork is beginning to pile up on your desk, and tensions at the workpl ...
- [一]初识Poi
示例代码: package com.lxl.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSS ...
- bootstrap-table对前台页面表格的支持
1.bootstrap-table是在bootstrap的基础上面做了一些封装,所以在使用bootstrap-table之前要导入的js和css有 1)基本的还是jQuery <script t ...
- Centos下安装FTP并进行虚拟用户访问方式配置
1. 安装认证所需包 [root@localhost]#yum install pam* [root@localhost]#yum install db4* 首先安装PAM(用于用户认证)和DB4(用 ...
- lambda显式声明返回值
10.21 编写一个lambda,捕获一个局部int变量,并递减变量值,直至它变为0.一旦变量变为0,再调用lambda应该不再递减变量.lambda应该返回一个bool值,指出捕获的变量是否为0. ...
- Thread和Runnable、run和start的区别
多线程可以通过两种方式来创建: 一.通过继承Thread类. 二.通过实现Runnable接口. 那么中两种方式到底有什么区别呢?那种方式更好些呢? 先看看几个简单的Demo: Demo1 publi ...