hdu 1242 Rescue (BFS)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12927 Accepted Submission(s): 4733
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
//31MS 356K 1410 B C++
/* 题意:
从点a到点r,求最短步数 BFS:
典型的BFS,这题数据有点怪.最好用优先队列实现bfs。 */
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int mov[][]={,,,,-,,,-};
char map[][];
int bx,by,n,m;
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
void bfs(int x,int y)
{
priority_queue <node> Q;
node t={x,y,};
Q.push(t);
while(!Q.empty())
{
t=Q.top();
Q.pop();
for(int i=;i<;i++)
{
node tt=t;
tt.x+=mov[i][];
tt.y+=mov[i][];
tt.step++;
if(tt.x>=&&tt.x<n&&tt.y>=&&tt.y<m&&map[tt.x][tt.y]!='#')
{
if(map[tt.x][tt.y]=='r')
{
cout<<tt.step<<endl;
return;
}
else if(map[tt.x][tt.y]=='x')
tt.step++;
map[tt.x][tt.y]='#';
Q.push(tt);
}
}
}
cout<<"Poor ANGEL has to stay in the prison all his life.\n";
}
int main(void)
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
bx=i,by=j;
}
bfs(bx,by);
}
return ;
}
hdu 1242 Rescue (BFS)的更多相关文章
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- 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 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- 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)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu 1242 Rescue(BFS,优先队列,基础)
题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...
随机推荐
- 如何使用MongoDB+Springboot实现分布式ID?
转载请标明出处: http://blog.csdn.net/forezp/article/details/69056017 本文出自方志朋的博客 一.背景 如何实现分布式id,搜索相关的资料,一般会给 ...
- JavaScript变量不同类型之间的自动、手动类型转换
转换成字符型:toString() var str = 123; str.toString();转换成字符串 将str从数值型变成字符型 浮点数: 电脑在运算过程中以正确的 ...
- 牛客小白月赛2 J 美 【构造】
链接:https://www.nowcoder.com/acm/contest/86/J来源:牛客网 题目描述 最后,Sεlιнα(Selina) 开始了选美大赛. 一如既往地,Sεlιнα 想最大化 ...
- 【PGP公钥】
Fingerprint: 37AF 3814 3ABC 5DFA 97F5 300E 581D A2E3 F4D2 F585 Key ID:0x581DA2E3F4D2F585 -----BEGIN ...
- 使用Git将本地项目上传到码云
01.码云上创建一个项目 testgit (名字随意) 02.进入本地想要上传的目录,然后使用git bash 03.cd到想要上传的目录 04.$ git init 初始化一个git 本地仓库(项目 ...
- PHP siege 压测 QPS大小
1.使用 PHP-FPM SOCKET的形式通讯 2.配置 PHP-FPM配置 [root@bogon php-fpm.d]# ls -al 总用量 drwxr-xr-x. root root 8月 ...
- js如何获得系统时间年月日时分秒
javascript 自带有个对象(构造函数),Date().下面是代码: 回答一: var now = new Date(); var nowTime = now.toLocaleString() ...
- tcl之控制流-break/continue
- Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)
出现 Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)时在php.ini文件中配置 memor ...
- php 微信公众号图文消息回复的实现 与access_token
//代码如下 <?phpclass IndexAction extends Action { public function __construct(){ } public function i ...