Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19985    Accepted Submission(s):
7110

Problem Description
Angel was caught by the MOLIGPY! He was put in prison
by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There
are WALLs, ROADs, and GUARDs in the prison.

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.)

 
Input
First line contains two integers stand for N and
M.

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.

 
Output
For each test case, your program should output a single
integer, standing for the minimal time needed. If such a number does no exist,
you should output a line containing "Poor ANGEL has to stay in the prison all
his life."
 
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 
Sample Output
13
 

 

/*
题意 :公主被抓(为什么公主老是被魔王抓,烂梗),我要去救公主 (其实我不想救,心累)
a表示公主所在位置,r是我所在位置,#是墙,.是路,X是怪,打怪要两点疲劳值,走路要一点
疲劳值,问救出公主要多少点疲劳值救出公主的话输出疲劳值点数如果救不出输出
"Poor ANGEL has to stay in the prison all his life."
题解:同 poj2312(我相信细微的差别你们能改出来)
*/ #include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 210
using namespace std;
char map[MAX][MAX];
int n,m;
struct node
{
int x;
int y;
int time; friend bool operator < (node a,node b)//定义结构体的优先队列
{
return a.time>b.time;//花费时间少的先出队
} };
void bfs(int x1,int y1,int x2,int y2)
{
int j,i,ok=0; priority_queue<node>q;//定义结构 体优先队列 int move[4][2]={0,1,0,-1,1,0,-1,0};
node begin,end;
begin.x=x1;
begin.y=y1;
begin.time=0;
q.push(begin);
while(!q.empty())
{ end=q.top();//优先队列 q.pop();
if(end.x==x2&&end.y==y2)
{
ok=1;
break;
}
for(i=0;i<4;i++)
{
begin.x=end.x+move[i][0];
begin.y=end.y+move[i][1];
if(0<=begin.x&&begin.x<n&&0<=begin.y&&begin.y<m&&map[begin.x][begin.y]!='#')
{
if(map[begin.x][begin.y]=='x')
begin.time=end.time+2;
else
begin.time=end.time+1;
map[begin.x][begin.y]='#';
q.push(begin); }
}
}
if(ok)
printf("%d\n",end.time);//注意这里
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
int j,i,s,t,k,x1,x2,y1,y2;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]=='a')
{
x2=i;y2=j;
}
else if(map[i][j]=='r')
{
x1=i;y1=j;
}
}
}
bfs(x1,y1,x2,y2);
}
return 0;
}

  

hdoj 1242 Rescue的更多相关文章

  1. BFS HDOJ 1242 Rescue

    题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...

  2. 【HDOJ】1242 Rescue

    BFS+优先级队列. #include <iostream> #include <cstdio> #include <cstring> #include <q ...

  3. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  4. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  5. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

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

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

  7. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  8. HDU 1242 Rescue (BFS(广度优先搜索))

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

  9. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

随机推荐

  1. 1行代码为每个Controller自定义“TabBar”-b

    这篇文章大致会带你实现以下的功能,废话少说,先看东西: JPNavigationController.gif Q&A:Demo里都有那些东西? 01.关于自定义导航栏 01.第一个控制器的导航 ...

  2. xxx couldn't be loaded because it has not been added to the build settings.

    这个由于没有将进入场景放入Build Settings里面造成的.

  3. Cloud Test 单页面即时监测功能上线!

    什么是即时监测? 即时监测,顾名思义是指输入 URL 后能够立即进行监测并展示结果,无需注册. 如下图,在输入框内输入需要监测的 URL,点击免费监测,即可展示网页监测结果: 图中我们可以看到页面各个 ...

  4. linux下查看文件编码及修改编码

    http://blog.csdn.net/jnbbwyth/article/details/6991425 查看文件编码在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编 ...

  5. jenkin系列_调度jmeter实现分布式测试

    假设现在有 192.168.1.100(jmeter 控制器 C ).192.168.1.101(jmeter负载机 B)两台机器进行分布式测试,各个步骤如下 1. C 和B 安装jmeter,并运行 ...

  6. js设置radio选中

    在页面数据绑定时,经常会遇到给radio设置选中,以下是我写的js方法,经测试可以使用.欢迎拍砖 <html> <head> <script type="tex ...

  7. 10. 将摄像机对准物体,并显示整个对准过程,摄像机Zoom

    1. 如果把代码放到按钮事件中调用,达不到想要的效果 2. 可以不用委托,但是要在Update函数中写调用CameraZoonIn的代码 3. 有很多需要改进的地方,可以参考使用 iTween 插件达 ...

  8. Altium Designer中默认取消重复画线的选项

  9. Android 透明Button

    1.是制作9-patch的图片,这样能够匹配文字内容的长短. 2.是指定按钮样式背景,即定制drawable的xml文件,这样做的好处不用图片做背景,节省空间. 定制透明样式的按钮.直接看代码: dr ...

  10. 在C++中子类继承和调用父类的构造函数方法

    构造方法用来初始化类的对象,与父类的其它成员不同,它不能被子类继承(子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法).因此,在创建子类对象时,为了初始化从父类继承来的数据成员,系统需 ...