看题传送门:

ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649

HDU http://acm.hdu.edu.cn/showproblem.php?pid=1242

题目大意初始位置在r,要求到达a的地点,地图上"."通过需要1s,“x"代表守卫,通过耗时2s,“#”不能走。

BFS的应用。

BFS求最短路径的原理是每一次向外扩张一格,(就像树的层次遍历一样),生成的BFS树把同一步数放于同一层,故能保证最优解。

这题变形之处在于,迷宫上有守卫,打到守卫需要1s,才能进入守卫所在的格子。

故不能简单的BFS。

1.

我想到的方法是:

用一个数组记录当前到达的最小步数,如果走过一个格子,下一次走过的时间比较短,那么入队列,更新数组。否则跳过。

2.

看了别人的方法还有:

先在进入守卫所在的地方然后干掉守卫。

如果当前的格子是x则把步数+1,还有把x改为.   并重新入队列。

剩下的就是简单的BFS。

3.

优先队列。。。。Orz大牛。

把队列里面的各个点的步数从小到大,先到达目标的一定是最优的。。。。Orz

方法一:BFS+剪枝。

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN=200+10;
const int INF=999999;
char maze[MAXN][MAXN];
int vis[MAXN][MAXN];
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
int N,M;
struct site
{
int x,y;
int step;
site(int i,int j,int s){x=i;y=j;step=s;}
site(){}
}start,en; queue<site> q;
void bfs()
{ q.push(start);
int i;
while(!q.empty())
{
site cur=q.front();
//等下试试temp,而不是q.push(site(nx,ny));
q.pop();
for(i=0;i<4;i++)
{
int nx=cur.x+dx[i];
int ny=cur.y+dy[i];
int ns=cur.step; if( nx>=0 && ny >=0 && nx < N && ny < M && maze[nx][ny]!='#' )
{ if(maze[nx][ny]=='a' && ns+1 < en.step )
{
en.step=ns+1;
continue;
} if(maze[nx][ny]=='.'&& ns + 1 < vis[nx][ny] ) //剪枝
{
vis[nx][ny]=ns+1;
q.push(site(nx,ny,ns+1));
} if (maze[nx][ny]=='x' && ns + 2 < vis[nx][ny] )
{
vis[nx][ny]=ns+2;
q.push(site(nx,ny,ns+2));
}
}
}
} } int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
while(!q.empty())
q.pop(); getchar();
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%c",&maze[i][j]);
if(maze[i][j]=='r')
{
start.x=i;
start.y=j;
start.step=0;
}
else if(maze[i][j]=='a')
{
en.x=i;
en.y=j;
en.step=INF;
}
vis[i][j]=INF;
}
getchar();
} bfs(); if(en.step==INF)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",en.step);
}
}

方法二:先干掉守卫

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN=200+10;
const int INF=999999;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
int N,M;
struct site
{
int x,y;
int step;
site(int i,int j,int s){x=i;y=j;step=s;}
site(){}
}start,en; queue<site> q;
void bfs()
{
memset(vis,0,sizeof(vis));
q.push(start);
int i;
while(!q.empty())
{
site cur=q.front();
q.pop(); if(maze[cur.x][cur.y]=='x')
{
maze[cur.x][cur.y]='.';
cur.step++;
q.push(cur);
continue;
} for(i=0;i<4;i++)
{
int nx=cur.x+dx[i];
int ny=cur.y+dy[i];
int ns=cur.step; if( nx>=0 && ny >=0 && nx < N && ny < M && maze[nx][ny]!='#' &&!vis[nx][ny])
{ if(maze[nx][ny]=='a' )
{
en.step=ns+1;
return;
}
//此时无需判断是否为x或者.,因为我们是先干掉守卫然后在进入守卫所在的地方。
q.push(site(nx,ny,ns+1)); vis[nx][ny]=true;
}
}
}
} int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
while(!q.empty())
q.pop(); getchar();
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%c",&maze[i][j]);
if(maze[i][j]=='r')
{
start.x=i;
start.y=j;
start.step=0;
}
else if(maze[i][j]=='a')
{
en.x=i;
en.y=j;
en.step=INF;
}
}
getchar();
} bfs(); if(en.step==INF)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",en.step);
}
}

方法三: 优先队列

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=200+10;
const int INF=0x3ffffff;
char map[MAXN][MAXN];
bool vis[MAXN][MAXN];
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1}; struct node
{
int step,x,y;
node(){}
node(int x,int y,int step): x(x),y(y),step(step){}
bool operator < (const node&a)const{
return step>a.step;
}
}s,e; void bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<node> q;
q.push(s);
vis[s.x][s.y]=true;
while(!q.empty())
{
node cur=q.top();
q.pop();
for(int i=0;i<4;i++)
{
int nx=cur.x+dx[i],ny=cur.y+dy[i];
if(map[nx][ny]=='a')
{
e.step=cur.step+1;
return;
}
if(map[nx][ny]!='#'&&!vis[nx][ny])
{
if(map[nx][ny]=='x')
{
q.push(node(nx,ny,cur.step+2));
map[nx][ny]='.';
vis[nx][ny]=true;
}
else
{
q.push(node(nx,ny,cur.step+1));
vis[nx][ny]=true;
}
}
}
}
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
{
scanf("%s",map[i]+1);
for(int j=1;j<=m;j++)
{
if(map[i][j]=='a')
{
e.x=i;e.y=j;e.step=INF;
}
if(map[i][j]=='r')
{
s.x=i;s.y=j;s.step=0;
}
}
} for(int i=0;i<=m+1;i++)
map[0][i]=map[n+1][i]='#';
for(int i=0;i<=n+1;i++)
map[i][0]=map[i][m+1]='#'; /* for(int i=0;i<=n+1;i++)
{
for(int j=0;j<=m+1;j++)
printf("%c",map[i][j]);
printf("\n");
}*/
bfs();
if(e.step==INF)
puts("Poor ANGEL has to stay in the prison all his life.");
else
printf("%d\n",e.step);
}
return 0;
}

ZOJ-1649 Rescue BFS (HDU 1242)的更多相关文章

  1. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

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

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

  3. zoj 1649 Rescue

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

  4. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  5. BFS zoj 1649

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

  6. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  7. HDU 1242 Rescue(BFS),ZOJ 1649

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

  8. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  9. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

随机推荐

  1. NOI2017整数

    NOI2017 整数 题意: ​ 让你实现两个操作: 1 \(a\) \(b\):将\(x\)加上整数\(a \cdot 2 ^ b\),其中 \(a\)为一个整数,\(b\)为一个非负整数 2 \( ...

  2. 一个简单RPC框架是怎样炼成的(II)——制定RPC消息

    开局篇我们说了,RPC框架的四个核心内容 RPC数据的传输. RPC消息 协议 RPC服务注冊 RPC消息处理 以下,我们先看一个普通的过程调用 class Client(object): def _ ...

  3. read()方法读取的是一个字节,为什么返回是int,而不是byte

    因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到111111111                 那 ...

  4. 7.Maven之(七)pom.xml配置文件详解

    转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...

  5. vue2.0实现银行卡类型种类的选择

    功能效果:vue2.0实现银行卡类型种类的选择 图片.png 参考代码如下: <template> <div class="app"> <header ...

  6. 进阶攻略|最全的前端开源JS框架和库

    新的 Javascript 库层出不穷,从而Web 社区愈发活跃.多样.在多方面快速发展.详细去描述每一种主流的 Javascript框架和库近乎不可能,所以在这篇文章中主要介绍一些对前端发展最具影响 ...

  7. TypeScript深入学习

    基础类型booleannumberstringstring[]//Array<string> 数组类型(ReadonlyArray<string>数组不能修改,也不允许被赋值给 ...

  8. Struts1 的html标签的具体解说与使用

    <html:form> 标签 <html:form>用来创建表单.<html:form>必须包括一个action属性,否则JSP会抛出一个异常. 经常使用的属性有下 ...

  9. linux下多进程的文件拷贝与进程相关的一些基础知识

    之前实现了用文件IO的方式能够实现文件的拷贝,那么对于进程而言,我们是否也能够实现呢? 答案是肯定的. 进程资源: 首先我们先回想一下,进程的执行须要哪些资源呢?其资源包含CPU资源,内存资源,当然还 ...

  10. C语言速度优化之指针赋值与if推断

    近期在写的一个项目须要优化处理速度,我写了一下程序来測试指针赋值与指针推断的速度比較.结果让我大吃一惊. #include <stdio.h> #include <stdlib.h& ...