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 ...
随机推荐
- JDK 动态代理 讨债实例
现弄一个讨债接口 package cn.itcast.g_dongtaidaili; public interface Taozhai { void taozhai(); } 再弄一个讨债实现类 pa ...
- Python——字典
字典是一种key-value 的 数据类型,使用就想我们上学用的字典.可以通过笔画,字母来查对应页的详细内容. 特性:1. 字典是无须的.(如果光打印字典里的字符串,那么排序不会按照顺序排,因为字典是 ...
- struts2之输入验证
输入校验主要分为两种: 基于客户端的校验: 客户端校验主要作用是防止正常浏览者的误输入,仅能对输入进行初步过滤:对于一些用户恶意行为,客户端校验则无能为力. 基于服务端的校验: 服务器接收客户端提交的 ...
- kali linux渗透系统的安装
Kali 安装详细步骤 实验环境 Windows:Windows 10 企业版 VMware:VMware Workstation 12 Pro Kali:kali-linux-2016.2-am ...
- C语言函数篇(一)函数的组成
函数的组成: 函数名 输入参数 返回值 返回值 函数名 (输入参数){ 执行体 } 用指针保存函数: int func(int a, int b, char c){ } --> int (*fu ...
- CF961E Tufurama 树状数组
E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...
- 浅谈XX系统跨平台迁移(测试环境)
一 概述 XX系统目前运行在XX-A的云平台上,计划将其迁移至XX-B的云平台. XX系统是java开发,中间组件涉及nginx+keepalived实现各个业务系统之间的高可用,kafka,zook ...
- Android 自定义WebView 实现可以加载缓存数据
1.自定义WebView说明 1.1.这个WebView可以加载缓存的数据.(需要后端配合,将html转换成一个字符串,主要是图片要用特殊格式) 1.2.注入了图片链接,为了方便点击webView中的 ...
- Kali2017 Metasploit连接postgresql数据库
msfdb:msf数据库管理命令 1.查看msf数据库连接状态 msf > db_status [*] postgresql selected, no connection //未连接 2.ms ...
- 利用 ESLint 检查代码质量
原文发表于作者的个人博客:http://morning.work/page/maintainable-nodejs/getting-started-with-eslint.html 其实很早的时候就想 ...