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. ...
随机推荐
- A题进行时--浙大PAT 1001-1010
pat链接:http://pat.zju.edu.cn 1 #include<stdio.h> 2 int main(){ 3 int a,b; 4 int c; 5 while(scan ...
- Floyd-Warshall算法的理解
Floyd算法可以求图内任意两点之间的最短路径,三重循环搞定,虽然暴力,但是属于算法当中最难的动态规划的一种,很有必要理解. 花了一晚上和半个下午专门看这个,才看个一知半解,智商被碾压没办法. 我一直 ...
- HW4.2
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- UVALive 5111 Soccer Teams (动态规划)
题意:给指定数量的数字“1”,“2”,“3”……,“9”.用所有这些数字加上任意个0组成一个数,要求数能被11整除,且数的位数尽量小. 能被11整除的数有一个特点,奇数位数字之和与偶数位之和的差为11 ...
- 仿写thinkphp的I方法
自己定义一个方法仿写thinkphp的I 方法 <?php function I($key="",$default='',$function="") { ...
- 写一些有关android的东西吧,那时候玩android时候的一些笔记
写一些有关android的东西吧,那时候玩android时候的一些笔记
- PHP class_exists 检查类是否已定义
(PHP 4, PHP 5) class_exists — 检查类是否已定义 bool class_exists ( string $class_name [, bool $autoload ] ) ...
- Android Studio中使用Gradle打包
首先要注意一点,Android Studio中把proguard.txt已经命名为proguard-rules.pro,由此可见,採用Gradle打包,混淆规则文件的名称是不重要的.能够自己随便命名. ...
- 在TextView使用部分颜色文字
/** * change a part of string color. * * @param string * whole string. * @param subString * the sub ...
- [GIF] GIF Loop Coder - Interpolation
This video discusses the default interpolation in GIF Loop Coder, and four distinct ways to change t ...