解题心得:

1.读清楚题意,本题的题意是有多个‘r’(起点),多个r多个bfs比较最短的时间即可,但是hdoj的数据比较水,直接一个起点就行了,迷宫里有多个守卫,如果在路途中遇到守卫会多花费一个时间点,求最短时间救到公主。

2.(解法一)因为遇到守卫会多花费一个时间,所以在守卫的地方再次压入,但是时间加一,这样就可以让队列里面的先验证。

3.之前将此题理解为要将所有的守卫打败之后,才能救到天使,之前题意理解错误用了回溯法,找出所有打败全部守卫的情况,找到最短的路径。此题的数据0<n<m<=200,很明显的会超时。

4.(解法二)使用优先队列,虽然看起来和解法一相差不大,但是如果打败守卫的时间不同那么解法二将明显更方便。

题目:

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 29754    Accepted Submission(s): 10489

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

 

Author

CHEN, Xue



解法一代码:
#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
int x,y,step;
}now,Next;
char maps[210][210];
int use[210][210],n,m;
int dir[4][2]={1,0,-1,0,0,1,0,-1}; bool check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m || use[x][y] || maps[x][y] == '#')
return false;
else
return true;
}
void maps_store()
{ for(int i=0;i<n;i++)
{
scanf("%s",maps[i]);
for(int j=0;j<m;j++)
{
if(maps[i][j] == 'r')
{
now.x = i;
now.y = j;
now.step = 0;
}
}
}
} void bfs()
{
queue <node> qu;
qu.push(now);
use[now.x][now.y] = 1;
while(!qu.empty())
{
now = qu.front();
qu.pop();
if(maps[now.x][now.y] == 'a')
{
printf("%d\n",now.step);
return ;
}
if(maps[now.x][now.y] == 'x')
{
maps[now.x][now.y] = '.';
now.step = now.step + 1;
use[now.x][now.y] = 1;
qu.push(now);//再次压入
continue;
}
for(int i=0;i<4;i++)
{
Next.x = now.x + dir[i][0];
Next.y = now.y + dir[i][1];
Next.step = now.step + 1;
if(check(Next.x,Next.y))
{
use[Next.x][Next.y] = 1;
qu.push(Next);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(use,0,sizeof(use));
maps_store();
bfs();
}
}


解法二代码:


#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxsize = 210;
int n,m,dir[4][2] = {1,0,-1,0,0,1,0,-1},use[maxsize][maxsize];
char map[maxsize][maxsize];
struct node
{
int x,y,time;
friend bool operator < (node a,node b)//队列中排序的判断,注意写法
{
return a.time>b.time;
}
} now,Next; priority_queue <node> pr_qu;//优先队列 void map_store()
{
for(int i=0; i<n; i++)
cin>>map[i];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(map[i][j] == 'r')
{
now.x = i;
now.y = j;
now.time = 0;
use[now.x][now.y] = 1;
return;
}
}
}
} bool check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m || map[x][y] == '#' || use[x][y] == 1)
return false;
else
return true;
} int bfs()
{
while(!pr_qu.empty())
pr_qu.pop();
pr_qu.push(now);
while(!pr_qu.empty())
{
now = pr_qu.top();
if(map[now.x][now.y] == 'a')
return now.time;
for(int i=0; i<4; i++)
{
Next.x = now.x + dir[i][0];
Next.y = now.y + dir[i][1];
if(check(Next.x,Next.y))
{
if(map[Next.x][Next.y] == 'x')//注意这部分的位置
Next.time = now.time + 2;
else
Next.time = now.time + 1;
pr_qu.push(Next);
use[Next.x][Next.y] = 1;
}
}
pr_qu.pop();
}
return -1;//没有找到a时返回-1
}
int main()
{
while(cin>>n>>m)
{
memset(use,0,sizeof(use));
map_store();
int Time = bfs();
if(Time == -1)
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<Time<<endl;
}
}

BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)的更多相关文章

  1. hdu 1242 Rescue (BFS)

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

  2. hdu 1242 Rescue

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

  3. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

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

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

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

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

  6. hdu 1242 Rescue(bfs)

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

  7. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  8. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

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

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

随机推荐

  1. POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】

    Frogger Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  2. Unity GL 画圆

    Unity下GL没有画圆的函数,只能自己来了. 如果能帮到大家,我也很高兴. 虽然没有画圆的函数,但是能画直线,利用这一点,配合微积分什么的,就可以画出来了.反正就是花很多连在一起的直线,每条直线足够 ...

  3. js 独立命名空间,私有成员和静态成员

    独立的命名空间   1可以避免全局变量污染. 全局变量污染不是 说 被全局变量污染,而是说不会污染全局变量.   2实现私有成员. 在js中函数 就可以满足独立的命名空间的两点需求.   如:     ...

  4. JAVA4大线程池

    不知不觉中我们电脑的硬件设施越来越好,从双核四线程普及到如今四核八线比比皆是.互联网发展至今,讲究的就是快,less is more,而且大数据的诞生和各种种类繁多的需求处理,单线程的程序逐渐不能满足 ...

  5. mysql set names 命令和 mysql字符编码问题

    先看下面的执行结果: (root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------- ...

  6. Validation failed for one or more entities. See ‘EntityValidationErrors’,一个或多个验证错误 解决方法

    try{// 写数据库}catch (DbEntityValidationException dbEx){ }在 dbEx 里面中我们就可以看到

  7. GoDaddy虚拟主机创建FTP 图文流程

    有了ftp各种操作就方便多了,也不用通过网页的控制面板来修改代码了 狗爹linux虚拟主机创建FTP 1. 通过虚拟主机管理界面,进入cPanel控制面板 2. 进入FTP管理页面 3. 填写账号.密 ...

  8. mysql 5.7以上安装遇到的问题

    参考地址:  https://blog.csdn.net/u012278016/article/details/80455439 本人在window上安装mysql 5.7版本以上的mysql,出现很 ...

  9. Spring MVC + Thymeleaf

    参考网址: https://www.cnblogs.com/litblank/p/7988689.html 一.简介 1.Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应 ...

  10. System Center Configuration Manager 2016 域准备篇(Part4)

    步骤4.创建系统管理容器 注意:在Active Directory域控制器服务器(AD01)上以本地管理员身份执行以下操作 有关您为何这样做的详细信息,请参阅https://docs.microsof ...