又是类似骑士拯救公主,不过这个是朋友拯救天使的故事。。。

不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~

求到达天使的最短时间,杀死一个护卫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)(转载)的更多相关文章

  1. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  2. zoj 1649 Rescue

    BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...

  3. BFS zoj 1649

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...

  4. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  5. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  6. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. zoj 1649 bfs

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  8. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

随机推荐

  1. HW4.8

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  2. ssh -CT -o BatchMode=yes 用户名@主机名

  3. 问题-Delphi编译到最后Linking时总是出现与ntdll.dll有关的错误还有Fatal Error Out of memory错误

    1.跳出错误法  ===================================================在主界面的implementation  {$R *.dfm} 下放入以下代码: ...

  4. Laravel教程:laravel 4安装及入门

    一.安装Composer首先你需要安装Composer,Composer是PHP依赖管理工具,Laravel框架就是使用 Composer 执行安装和依赖管理. 注:(1)若安装Composer出错, ...

  5. Xcode5 上使用Base SDK iOS6程序和iOS6模拟器

    Xcode 5默认自带SDK 7.0,升级Xcode 5后,Xcode.5就没了,这样我想编译SDK 6.x的程序就难办了(除非同时安装Xcode 4.x和Xcode 5.x两个版本Xcode).其中 ...

  6. Java+7入门经典 -2 数据

    第2章 程序,数据,变量和计算 2.1 数据和变量 变量是一段有名字的内存, 存储程序中的信息, 描述事物的数据项; 每段定义了名字的内存只能存储一种特定类型的数据. Type; 编译器会检测错误的类 ...

  7. 【S13】vector和string优先于动态分配的内存

    1.使用new动态分配内存,必须承担如下责任: a.使用delete释放内存: b.确保使用了正确的形式,delete与new的形式要匹配: c.不能重复delete. 2.使用vector和stri ...

  8. Executing System commands in Java---ref

    One of the nice features of Java language is that it provides you the opportunity to execute native ...

  9. 打造强大的BaseModel(2):让Model实现自动映射,将字典转化成Model

    打造强大的BaseModel(1):让Model自我描述 这篇文章将讲述Model一项更高级也最常用的功能,让Model实现自动映射–将字典转化成Model(所有代码全由Swift实现) 将JSON转 ...

  10. To Be NUMBER ONE

    Problem Description One is an interesting integer. This is also an interesting problem. You are assi ...