Rescue

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

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
 

这个题是用广搜写的!但是这个题是有点特殊的,因为遇到X的时候时间是要再加一的!所以可以用优先队列来解决,但是还是可以用普通队列来解决的,方法就是遇到X先加一,然后标记走过,再后来再遇到X直接加一,不再往四周搜索!等于走了两步!详情如下

普通队列版:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ma 210
using namespace std;
char map[ma][ma];
int v[ma][ma],m,n;
int mov[][]={,,-,,,,,-};
struct node
{
int x,y,step,flag;
};
bool can(node x)
{
if(x.x>=&&x.x<m&&x.y>=&&x.y<n&&(map[x.x][x.y]!='#')&&!v[x.x][x.y])
return true ;
return false;
}
int bfs(int x,int y)
{
int i;
memset(v,,sizeof(v));
queue<node>q;
node ta,tb;
ta.x=x;
ta.y=y;
ta.step=;
ta.flag=;
q.push(ta);
while(!q.empty())
{
ta=q.front();
q.pop();
if(ta.flag==)
{
ta.step++;
ta.flag=;
q.push(ta);
continue;
}//第二次遇到时,flag已经等于1,这时候不能走了,步数要加一,而且flag要重新标记为0
if(map[ta.x][ta.y]=='a')
return ta.step;
for(i=;i<;i++)
{
tb.x=ta.x+mov[i][];
tb.y=ta.y+mov[i][];
tb.step=ta.step+;
if(can(tb))
{ if(map[tb.x][tb.y]=='x')
tb.flag=;
else
tb.flag=;//第一次遇到X就标记为flag=1,否则为0!
v[tb.x][tb.y]=;
q.push(tb);
}
}
}
return -;
}
int main()
{
int i,j,a,b;
while(scanf("%d%d",&m,&n)!=EOF)
{
getchar();
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='r')
{
a=i;
b=j;
}
}
getchar();
}
v[a][b]=;
int rr=bfs(a,b);
if(rr==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",rr);
}
return ;
}

优先队列版:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 210
using namespace std;
int m,n,v[maxn][maxn],mov[][]={,,-,,,,,-};
char map[maxn][maxn]; struct node
{
int x,y,step;
friend bool operator <(node x,node y)
{
return x.step>y.step;
}
};
priority_queue<node>q;
bool can(node x)
{
if(!v[x.x][x.y]&&x.x>=&&x.x<m&&x.y>=&&x.y<n&&map[x.x][x.y]!='#')//(map[x.x][x.y]=='.'||map[x.x][x.y]=='x'))
return true;
return false;
}
int bfs(int x,int y)
{
int i;
node ta,tb;
ta.x=x;
ta.y=y;
ta.step=;
q.push(ta);
while(!q.empty())
{
ta=q.top();
q.pop();
if(map[ta.x][ta.y]=='a')
return ta.step;//到终点就返回队首,用的优先队列所以队首的步数最少!
for(i=;i<;i++)
{
tb.x=ta.x+mov[i][];
tb.y=ta.y+mov[i][];
if(can(tb))
{
if(map[tb.x][tb.y]=='x')
tb.step=ta.step+;//遇到X一定要加2
else
tb.step=ta.step+;//其他加1
v[tb.x][tb.y]=;
q.push(tb);
} }
}
return -;//找不到就返回-1
}
int main()
{
int i,j,a,b;
while(scanf("%d%d",&m,&n)!=EOF)
{
getchar();
memset(v,,sizeof(v));
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='r')
a=i,b=j;
}
getchar();
}
v[a][b]=;
int rr=bfs(a,b);
if(rr==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",rr);
}
return ;
}

Rescue--hdu1242的更多相关文章

  1. Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)

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

  2. HDU1242 Rescue

    Rescue Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Description A ...

  3. HDU1242 Rescue(BFS+优先队列)

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

  4. hdu1242 Rescue DFS(路径探索题)

    这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...

  5. 搜索专题: HDU1242 Rescue

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

  6. hdu1242 Rescue bfs+优先队列

    直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...

  7. hdu1242 Rescue(BFS +优先队列 or BFS )

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:     Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...

  8. Nova Suspend/Rescue 操作详解 - 每天5分钟玩转 OpenStack(35)

    本节我们讨论 Suspend/Resume 和 Rescue/Unrescue 这两组操作. Suspend/Resume 有时需要长时间暂停 instance,可以通过 Suspend 操作将 in ...

  9. HDU1242 BFS+优先队列

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

  10. HDU-4057 Rescue the Rabbit(AC自动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. nRF51 DFU 初始化包介绍及生成工具

    nRF51 DFU 初始化包 当升级数据包时,在应用程序映像传输之前,在DFU中需要初始化包来执行映像的安全检测.这个初始化包作为升级流程的一部分提供了安全检测机制,因此被升级的设备只能接收兼容的应用 ...

  2. OpenUrl 的跨平台实现

    OpenUrl 是 iOS 中 UIApplication 提供的一个函数,用于调用其它程序.实际上各个平台都有自己的实现,这里提供一个直接封装完的跨平台版本给大家.           Delphi ...

  3. 原型模式 - OK

    原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 简单说来原型模式就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节. 原型模式UML ...

  4. 针对portmap 的DDOS攻击

    iptables -I INPUT -p tcp --dport 111 -j DROP iptables -I INPUT -s 10.171.254.221 -p tcp --dport 111 ...

  5. VS2012/2013编辑器问题

    1. Visual Studio 2013 'Could not evaluate Expression' Debugger Abnormality 解决办法:http://weblog.west-w ...

  6. MySql按日期时间段进行统计(前一天、本周、某一天、某个时间段)

    在mysql数据库中,常常会遇到统计当天的内容.例如,在user表中,日期字段为:log_time 统计当天 sql语句为: select * from user where date(log_tim ...

  7. 常用文件的文件头(附JAVA测试类)

    1. MIDI (mid),文件头:4D546864 2. JPEG (jpg),文件头:FFD8FF 3. PNG (png),文件头:89504E47 4. GIF (gif),文件头:47494 ...

  8. pyqt cvs保存

    # -*- coding: utf-8 -*-__author__ = 'Administrator'import sys, csvfrom PyQt4 import QtGui, QtCore cl ...

  9. (转)Android中截取当前屏幕图片

    该篇文章是说明在Android手机或平板电脑中如何实现截取当前屏幕的功能,并把截取的屏幕保存到SDCard中的某个目录文件夹下面.实现的代码如下: /** * 获取和保存当前屏幕的截图 */ priv ...

  10. AngularJs学习笔记2——四大特性之MVC

    angularJs的四大特性 ①.采用MVC的设计模式 ②.双向数据绑定 ③.依赖注入 ④.模块化设计 现在细说一下MVC的设计模式: MVC: Model(模型)--项目中的数据 View(视图)- ...