ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门:
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)的更多相关文章
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- 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 ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- BFS zoj 1649
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...
- 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 / ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- hdu 1242 Rescue(bfs)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
随机推荐
- JavaScript--数据结构与算法之链表实现约瑟夫环
传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第三个人时将 ...
- 【D3 API 中文手冊】
[D3 API 中文手冊] 声明:本文仅供学习所用,未经作者同意严禁转载和演绎 <D3 API 中文手冊>是D3官方API文档的中文翻译. 始于2014-3-23日,基于VisualCre ...
- 制作U盘启动盘将Ubuntu 12.04升级为14.04的方法
1 介绍 在周六的下午,我决定想高速浏览一下书籍[1].看看这个关于Ubuntu的圣经到底在讲什么东东. 感觉讲的不错,当我看到介绍文件标记语言-TeX和LaTeX的时候,该书作者推荐在Ubuntu上 ...
- SQL数值转字符串保留指定小数位
IF EXISTS ( SELECT * FROM sysobjects WHERE xtype = 'fn' AND name = 'fn_NumberFormat' ) BEGIN DROP FU ...
- 最简单的实体手机测试移动端前端Vue Cli3搭建网站的方法
手机和PC同用一个路由的情况下,直接在手机的浏览器上输入Ip: 192.168.1.100:8080 就能看到了. 其中192.168.1.100是PC的IP.不同的自己改下就好. 就这么简单.啥都不 ...
- JS 原型模式创建对象
例子: class Test { constructor(val) { this.val = val } walk() { console.log(this) console.log('walk') ...
- BZOJ3510首都(LCT)
Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从 ...
- 关于后台接收参数为null的问题之ajax--contentType
ajax方法中的参数: contentType:发送至服务器时内容的编码类型,一般默认:application/x-www-form-urlencoded(适应大多数的场合) dataType:预期服 ...
- java和javascript日期校验和闰年问题分析和解决方式
1.闰年的介绍 地球绕太阳执行周期为365天5小时48分46秒(合365.24219天)即一回归年.公历的平年仅仅有365日,比回归年短约0.2422 日,所余下的时间约为四年累计一天.故四年于2月加 ...
- LinearLayout-layout_gravity 属性没有效果分析
今天在一个布局文件中,遇到了一个问题,先看代码 <LinearLayout android:layout_width="match_parent" android:layou ...