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)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
随机推荐
- DG性能
网络带宽 根据primary database redo产生的速率,计算传输redo需要的带宽. 出去tcp/ip网络其余30%的开销,计算需要的带宽公式: 需求带宽=((每秒产生redo的速率峰值/ ...
- C/C++(结构体)
结构体(struct) 从某种意义上说,会不会使用struct,如何使用struct是区别一个开发人员是否具备丰富开发经验的试金石. 处理由不同类型成员构成的构造类型,要采用结构体的方式. 定义:关键 ...
- celery work logging 问题
celery 的日志里只输出日志 不输入标准打印
- GetInvocationList 委托链表
最近发现C#程序初始化时在构造函数中,偶尔出现事件注册不成功.后查资料发现有GetInvocationList 这么一个获取类中的委托链表的函数, 使用方法如下: 1.在需委托的类(Class1)中增 ...
- 18/9/22NOIP模拟考
18/9/22NOIP模拟考 其实本来是有多组数据的,出题人忘记在题面上加了 斜眼笑 期望得分:100:实际得分:100 由于种种原因,拿到题的时候已经过去了0.5h+... 然后因为这道题数据范 ...
- HDU 4107 Gangster
Gangster Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 4 ...
- new不抛出异常nothrow与new_handler
可以看这里: http://blog.csdn.net/huyiyang2010/article/details/5984987 现在的new是会抛出异常的,bad::alloc 如果不想抛出异常两种 ...
- startActivityForResult()的用法
举例说我想要做的一个事情是,在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Act ...
- 【单词】常见单词含义的辨异(emulator/simulator、hardware/firmware)
1. emulator 与 simulator The Simulator tries to duplicate the behavior of the device.(仿真的是行为): The Em ...
- 25.C++多线程
#include <iostream> #include <thread> #include <Windows.h> using namespace std; vo ...