Rescue

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

Total Submission(s): 27126    Accepted Submission(s): 9607

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

解题思路:
公主被困了,需要解救,可以有好多个朋友去解救,最近的一个到达公主的房间就算解救成功。遇到士兵可以杀死,但是会额外多增加单位为1的时间。下面给出两种代码,BFS(普通队列)和BFS(优先队列)。截图第一个是BFS(优先队列)



BFS(普通队列)源代码:
<span style="font-size:18px;">//注意:步数最少的路线不一定花费时间最少的,同时当找到解不要立刻退出,
//因为找到的解有可能只能是步数最少解中的最优解,不一定是绝对意义上的最少时间
//不需要设置vis数组,也不用将做过的位置设置为墙壁,因为最少时间有下限,不会有无限循环
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<string>
#include<string.h>
#define INF 1000000
#define MAX 200
using namespace std; struct point //队列所需要要的机构体存储节点
{
int x, y; //位置
int step; //走到当前位置所用的步数
int time; //走到当前位置所花的时间
};
queue<point> Q; //头文件使用<queue>表示朋友所在的位置
int N, M;
char map[MAX][MAX]; //表示地图
int mintime[MAX][MAX];//每个位置所用的最少时间
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };//代表四个方向
int ax, ay; //angel的位置 int BFS(point s)
{
int i;
Q.push(s);
point hd;//从队列头出队列的位置
while (!Q.empty())//队列非空
{
hd = Q.front();
Q.pop();
for (i = 0; i < 4; i++)
{
int x = hd.x + dir[i][0];
int y = hd.y + dir[i][1];
//排除墙壁和边界
if (x < N&&x >= 0 && y < M&&y >= 0 && map[x][y] != '#')
{
point t;
t.x = x;
t.y = y;
t.step = hd.step + 1;
t.time = hd.time + 1;
if (map[x][y] == 'x')
t.time = t.time + 1;
if (t.time < mintime[x][y])
{
mintime[x][y] = t.time;
Q.push(t);
}
}
}
}
return mintime[ax][ay];
} int main()
{
int i, j;
while (scanf("%d%d", &N, &M) != EOF)
{
memset(map, 0, sizeof(map));//读入地图
for (i = 0; i < N; i++)
scanf("%s", map[i]);
int sx, sy;//朋友的位置
point start;//厨师的队列节点
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
if (map[i][j] == 'a')
{
ax = i;
ay = j;
}
else if (map[i][j] == 'r')
{
sx = i;
sy = j;
}
mintime[i][j] = INF;//寻找最小值,所以初始化为无穷大,并且有了更小的值才会替换
}
}
start.x = sx;
start.y = sy;
start.time = 0;
start.step = 0;
mintime[sx][sy] = 0;
int min = BFS(start);
if (min < INF)
printf("%d\n", min);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}</span>

BFS(优先队列)源代码:

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<functional>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std; typedef long long ll;
#define eps 1e-6
#define e exp(1.0)
#define pi acos(-1.0)
const int MAXN = 205;
const int MAXM = 205;
const int INF = 0x3f3f3f3f; typedef struct point
{
int x;
int y;
int time;
bool operator < (const point &p) const
{
return time > p.time;//取用时间占用较少的
}
}; char Map[MAXN][MAXM];
point start;
int n,m;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//方向 int BFS()
{
point pre;//原来的
point now;//现在的
priority_queue<point> q;
q.push(start);//把天使所在的位置压入
while(!q.empty())
{
pre=q.top();
q.pop();
for(int i=0;i<4;i++)
{
now.x=pre.x+dir[i][0];
now.y=pre.y+dir[i][1];
now.time=pre.time+1;
if(now.x<0||now.y<0||now.x>=n||now.y>=m)//越界
continue;
else if(Map[now.x][now.y]=='#')//墙壁
continue;
else if(Map[now.x][now.y]=='r')//朋友
return now.time;//找到朋友
else if(Map[now.x][now.y]=='.')
{
Map[now.x][now.y]='#';//改成墙壁
q.push(now);
}
else if(Map[now.x][now.y=='x'])//守卫
{
now.time++;
Map[now.x][now.y]='#';//改成墙壁
q.push(now);
}
}
}
return -1;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%c",&Map[i][j]);
if(Map[i][j]=='a')
{
start.x=i;
start.y=j;
start.time=0;
Map[i][j]='#';//顺便改成墙壁
}
}
getchar();
}
//输入成功
int res=BFS();
if(res==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",res);
}
return 0;
}
</span>


HDU-1242-Rescu的更多相关文章

  1. hdu 1242 Rescue

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

  2. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

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

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

  4. hdu 1242 Rescue(bfs)

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

  5. 杭电 HDU 1242 Rescue

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...

  6. HDU 1242 Rescue(优先队列)

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

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

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

  8. ZOJ-1649 Rescue BFS (HDU 1242)

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

  9. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  10. F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

随机推荐

  1. MongoDB关系与数据库引用

    MongoDB关系: MongoDB 的关系表示多个文档之间在逻辑上的相互联系.文档间可以通过嵌入和引用来建立联系. 1. 嵌入关系: 形式:把一个文档嵌入到另一个文档中. 优点:数据保存在单一的文档 ...

  2. Java多线程高并发学习笔记——阻塞队列

    在探讨可重入锁之后,接下来学习阻塞队列,这边篇文章也是断断续续的写了很久,因为最近开始学ssm框架,准备做一个自己的小网站,后续可能更新自己写网站的技术分享. 请尊重作者劳动成果,转载请标明原文链接: ...

  3. #1094 : Lost in the City by C solution

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...

  4. 《mysql必知必会》读书笔记--存储过程的使用

    以前对mysql的认识与应用只是停留在增删改查的阶段,最近正好在学习mysql相关内容,看了一本书叫做<MySQL必知必会>,看了之后对MySQL的高级用法有了一定的了解.以下内容只当读书 ...

  5. Coins(多重背包+二进制优化)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  6. 哈尔滨理工大学第六届程序设计团队 I-Team

    /* 以前做过一个插队的题,这个类似从后往前操作 */ #include <iostream> #include <stdio.h> #include <algorith ...

  7. ajax跨域请求解决方案

    大家好,今天我们学习了js的跨域请求的解决方案,由于JS中存在同源策略,当请求不同协议名,不同端口号.不同主机名下面的文件时,将会违背同源策略,无法请求成功!需要进行跨域处理! 方案一.后台PHP进行 ...

  8. rsync远程数据同步工具的使用

    准备工作 虚拟机1: 192.168.24.41, 用于搭建rsync服务器 虚拟机2: 192.168.26.68, 用于搭建rsync客户端 虚拟机1和虚拟机2均为centos7; 1. 检查虚拟 ...

  9. SpringMVC , Spring , MyBatis 文件上传

    学习一下文件上传下载,为图片上传做准备,感觉有一个世纪没玩过上传下载了,边敲代码边记录,请各路大神指教: 参考:http://blog.csdn.net/wjycgl/article/details/ ...

  10. 【Tesseract】Tesseract 的训练流程

    在泰迪杯A题中,我刚刚接触了Tesseact,其中训练字库中遇到了较多的问题.所以在此记录一下,也当做一个笔记,省得以后忘记. 为了方便 ,将tif命名格式设为[lang].[fontname].ex ...