Rescue(bfs)
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 11
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.)
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
#include<stdio.h>
#include<queue>
using namespace std;
struct Node{
int nx,ny,step;
friend bool operator < (Node a,Node b){
return a.step>b.step;
}
};
Node a,b;
char map[][];
int disx[]={,,-,};
int disy[]={,,,-};
int N,M,sx,sy,ex,ey,minstep,flot;
void bfs(){
priority_queue<Node>dl;
a.nx=sx;a.ny=sy;a.step=;
dl.push(a);
while(!dl.empty()){
a=dl.top();
dl.pop();
map[a.nx][a.ny]='#';
if(a.nx==ex&&a.ny==ey){flot=;
minstep=a.step;
return;
}
for(int i=;i<;i++){
b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+;
if(b.nx<||b.ny<||b.nx>=N||b.ny>=M||map[b.nx][b.ny]=='#')continue;
if(map[b.nx][b.ny]=='.'||map[b.nx][b.ny]=='r')dl.push(b);
else{
b.step++;dl.push(b);
}
}
}
}
int main(){
while(~scanf("%d%d",&N,&M)){minstep=;flot=;
for(int i=;i<N;i++)scanf("%s",map[i]);
for(int x=;x<N;x++){
for(int y=;y<M;y++){
if(map[x][y]=='a')sx=x,sy=y;
if(map[x][y]=='r')ex=x,ey=y;
}
}
bfs();
if(flot)printf("%d\n",minstep);
else puts("Poor ANGEL has to stay in the prison all his life.");
}
return ;
}
Rescue(bfs)的更多相关文章
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- hdu 1242 Rescue(bfs)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
随机推荐
- Word Ladder II 解答
Question Given two words (beginWord and endWord), and a dictionary's word list, find all shortest tr ...
- Maximum Subarray 解答
Question Find the contiguous subarray within an array (containing at least one number) which has the ...
- java中的TreeMap如何顺序按照插入顺序排序
java中的TreeMap如何顺序按照插入顺序排序 你可以使用LinkedHashMap 这个是可以记住插入顺序的. 用LinkedHashMap吧.它内部有一个链表,保持插入的顺序.迭代的时候,也 ...
- POJ3071:Football(概率DP)
Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2 ...
- git使用图解
使用前 安装git 配置name 和 email git config --global user.name "Your Name" git config --global use ...
- jar包和war包的区别:
jar包就是别人已经写好的一些类,然后将这些类进行打包,你可以将这些jar包引入你的项目中,然后就可以直接使用这些jar包中的类和属性了,这些jar包一般都会放在lib目录下. war是一个web模 ...
- DWZ在APS.NET WebForm中的使用(二)
任何框架由于个人理解不到位或者框架自身的局限性,在项目实施中,大家或多或少都会遇到一些问题,下面我就讲述下我在使用DWZ开发过程中的几个问题.如有一点能帮助到你,这篇文章也算有存在的意义了. 1.树菜 ...
- AUL使用初记
案例:部门有一个数据库因为机器无故重启,无法启动,初步判断是系统表空间出问题了.尝试过各种不同手段,均无法修复.后来发现上面只有一个用户的数据,遂想到直接通过AUL工具从数据文件中抽取出整个库. 准备 ...
- iOS开发那些事儿(一)轮播器
前言 市面上绝大部分的APP被打开之后映入眼帘的都是一个美轮美奂的轮播器,所以能做出一个符合需求.高效的轮播器成为了一个程序员的必备技能.所以今天的这篇博客就来谈谈轮播器这个看似简单的控件其中蕴含的道 ...
- B - Maya Calendar(第二季水)
Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old ...