时间限制: 1 Sec  内存限制: 64 MB
提交: 42  解决: 9
[提交][状态][讨论版]

题目描述

Angel被人抓住关在一个迷宫了!迷宫的长、宽均不超过200,迷宫中有不可以越过的墙以及监狱的看守。Angel的朋友带了一个救援队来到了迷宫中。他们的任务是:接近Angel。我们假设接近Angel就是到达Angel所在的位置。
假设移动需要1单位时间,杀死一个看守也需要1单位时间。到达一个格子以后,如果该格子有看守,则一定要杀死。交给你的任务是,最少要多少单位时间,才能到达Angel所在的地方(只能向上、下、左、右4个方向移动)?

输入

第1行两个整数n,m。表示迷宫的大小为n×m。
以后n行,每行m个字符。其中“#”代表墙,“.”表示可以移动,“x”表示看守,“a”表示Angel,“r”表示救援队伍。字母均为小写。

输出

l行,代表救出Angel的最短时间。如果救援小组永远不能达到Angel处,则输出“NO ANSWER”。

样例输入

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

样例输出

13

【分析】 前期做一些小小的处理,后面就是简单的BFS一下。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
using namespace std;
int n,m,flag=;
int dis[][]= {,,,,-,,,-};
int vis[][];
string w[];
struct man
{
int x,y,step;
};
queue<man>q;
void bfs(man s)
{
q.push(s);
while(!q.empty())
{
man t=q.front();
//printf("%d %d %d\n",t.x,t.y,t.step);
if(w[t.x][t.y]=='a'){printf("%d\n",t.step);flag=;return;}
else if(w[t.x][t.y]=='x'){t.step++;q.pop();q.push(t);w[t.x][t.y]='.';continue;}
q.pop();
for(int i=;i<;i++)
{
int xx=t.x+dis[i][];int yy=t.y+dis[i][];
if(xx>=&&xx<n&&yy>=&&yy<m&&vis[xx][yy]==&&w[xx][yy]!='#')
{
man k;
vis[xx][yy]=;
k.step=t.step+;k.x=xx;k.y=yy;q.push(k);
}
}
}
}
int main()
{
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&m);man s;
for(int i=;i<n;i++)cin>>w[i];
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
if(w[i][j]=='r')
{ s.x=i;
s.y=j;
s.step=;
}
}
bfs(s);
if(flag==)printf("NO ANSWER\n");
return ;
}

ViewCode

救援行动(save) (BFS)的更多相关文章

  1. 救援行动(save)

    救援行动(save) 题目描述 Angel被人抓住关在一个迷宫了!迷宫的长.宽均不超过200,迷宫中有不可以越过的墙以及监狱的看守.Angel的朋友带了一个救援队来到了迷宫中.他们的任务是:接近Ang ...

  2. 救援(BFS)

    题目描述: 在你的帮助下,Oliver终于追到小X了,可有一天,坏人把小X抓走了.这正是Oliver英雄救美的时候.所以,Oliver又找到哆啦A梦,借了一个机器,机器显示出一幅方格地图,它告诉Oli ...

  3. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. HDU2653 BFS+优先队列

    Waiting ten thousand years for Love Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. HDU1242 BFS+优先队列

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

  6. hdu.1254.推箱子(bfs + 优先队列)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  7. hdu1242 优先队列+bfs

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

  8. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. get( )与getline( )区别

    get与getline区别不是很大,但一个明显的区别是get遇到 '\n '字符后便返回,这是 '\n '还在缓冲区中,所以下次读出来的将是 '\n ',而getline遇到 '\n '也返回,但它会 ...

  2. [安卓]softap

    http://www.cnblogs.com/javawebsoa/archive/2013/05/29/3106758.html

  3. vSphere Client上传镜像

    1. 使用vSphere Client连接到VMware ESXI 2. 打开右侧 [摘要] 选项卡 3. 在 [资源] 中选择存储器中的存储,右键 [浏览数据库存储] 4. 选择工具栏 [创建新文件 ...

  4. 读取properties中的key对应的value

  5. Xcode如何查看内存中的数据

    在  debug 模式下如何在断点处,查看字符指针变量内存中的值,像vs2008的调试工具一样的内存查看器,现在只能查看第一个内存中的值可以在输出窗口采用gdb命令:x /nfu <addr&g ...

  6. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...

  7. 自己搭建Wifi Pineapple Mark V

    创业搞得自己很累,不过一切都是值得的.抽空写下文章,确实好久未更新了. 前段时间由于项目需要,所以就折腾了下wifi pineapple.时间间隔有点久,根据回忆记录下. 淘宝货:TP-Link TL ...

  8. Code is not literature

    http://www.gigamonkeys.com/code-reading/ I have started code reading groups at the last two companie ...

  9. NSNotificationCenter 的详细说明

    1. 定义一个方法 -(void) update{       } 2. 对象注册,并关连消息 [[NSNotificationCenter defaultCenter] addObserver:se ...

  10. 学习笔记:iPhone终极指南、手机端、浏览器各种规范

    手机图解参考:http://www.paintcodeapp.com/news/iphone-6-screens-demystified 手机图标尺寸参考:https://developer.appl ...