hdu 1242 Rescue(优先队列)
2016-01-26 10:12
429 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1242
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.)
[align=left]Input[/align]
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.
[align=left]Output[/align]
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."
[align=left]Sample Input[/align]
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
[align=left]Sample Output[/align]
13
题目大意:'a'代表公主的位置,'r'代表救公主的朋友的位置,'x'代表守卫的位置,每走一步需要1时间,杀死一个守卫也需要1时间,'#'是墙,可能有多个朋友,求救出公主的最短时间,如果没有路径可以到达公主,输出"Poor ANGEL has to stay in the prison all his life.";
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
#define N 310
#define met(a, b) memset (a, b, sizeof (a))
typedef long long LL;
//const int INF = ((1<<31)-1);
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char str
;
int k, n, m, vis
;
struct node
{
int x, y, t;
friend bool operator < (const node &a, const node &b)
{
return a.t > b.t;//时间短的先出队
}
}sa;
int BFS ()
{
priority_queue <node> que;//优先队列
que.push (sa);
met (vis, 0);
vis[sa.x][sa.y] = 1;
while (que.size())
{
node Sa = que.top(); que.pop();
if (str[Sa.x][Sa.y] == 'r') return Sa.t;
for (int i=0; i<4; i++)
{
node q = Sa;
q.x += dir[i][0], q.y += dir[i][1];
if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && !vis[q.x][q.y] && str[q.x][q.y]!='#')
{
q.t++;
vis[q.x][q.y] = 1;
if (str[q.x][q.y] == 'x')
q.t++;
que.push (q);
}
}
}
return -1;
}
int main ()
{
while (scanf ("%d %d", &n, &m) != EOF)
{
k = 0;
for (int i=0; i<n; i++)
{
getchar ();
for (int j=0; j<m; j++)
{
scanf ("%c", &str[i][j]);
if (str[i][j] == 'a')
sa = (node) {i, j, 0};
}
}
int ans = BFS ();
if (ans == -1) puts ("Poor ANGEL has to stay in the prison all his life.");
else printf ("%d\n", ans);
}
return 0;
}
Rescue
[align=left]Problem Description[/align]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.)
[align=left]Input[/align]
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.
[align=left]Output[/align]
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."
[align=left]Sample Input[/align]
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
[align=left]Sample Output[/align]
13
题目大意:'a'代表公主的位置,'r'代表救公主的朋友的位置,'x'代表守卫的位置,每走一步需要1时间,杀死一个守卫也需要1时间,'#'是墙,可能有多个朋友,求救出公主的最短时间,如果没有路径可以到达公主,输出"Poor ANGEL has to stay in the prison all his life.";
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
#define N 310
#define met(a, b) memset (a, b, sizeof (a))
typedef long long LL;
//const int INF = ((1<<31)-1);
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char str
;
int k, n, m, vis
;
struct node
{
int x, y, t;
friend bool operator < (const node &a, const node &b)
{
return a.t > b.t;//时间短的先出队
}
}sa;
int BFS ()
{
priority_queue <node> que;//优先队列
que.push (sa);
met (vis, 0);
vis[sa.x][sa.y] = 1;
while (que.size())
{
node Sa = que.top(); que.pop();
if (str[Sa.x][Sa.y] == 'r') return Sa.t;
for (int i=0; i<4; i++)
{
node q = Sa;
q.x += dir[i][0], q.y += dir[i][1];
if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && !vis[q.x][q.y] && str[q.x][q.y]!='#')
{
q.t++;
vis[q.x][q.y] = 1;
if (str[q.x][q.y] == 'x')
q.t++;
que.push (q);
}
}
}
return -1;
}
int main ()
{
while (scanf ("%d %d", &n, &m) != EOF)
{
k = 0;
for (int i=0; i<n; i++)
{
getchar ();
for (int j=0; j<m; j++)
{
scanf ("%c", &str[i][j]);
if (str[i][j] == 'a')
sa = (node) {i, j, 0};
}
}
int ans = BFS ();
if (ans == -1) puts ("Poor ANGEL has to stay in the prison all his life.");
else printf ("%d\n", ans);
}
return 0;
}
相关文章推荐
- iOS开发笔记--设置UITextField键盘上return key不可点击
- 一篇文章掌握RequireJS常用知识
- iOS开发笔记--UILabel的相关属性设置
- iOS开发笔记--UILabel和UITextView自适应文本高度代码
- iOS开发笔记--UIImageView的属性之animationImages详解
- 2016太原UI设计师郭文军图标的制作原则,面试必考
- iOS开发笔记--UILabel显示不同的字体和颜色
- iOS开发笔记--UIView中的坐标转换
- lightoj 1012 - Guilty Prince
- iOS应用UI开发中的字体和按钮控件使用指南
- IOS开发-UIView之动画效果的实现方法
- iOS UISearchController
- 《iOS Human Interface Guidelines》——Quick Look
- UIColor,CGColor,CIColor三者的区别和联系
- C#中SqlCommandBuilder批量数据增加修改
- 在 Windows 10 上安装 Arduino 开发环境
- Codeforces Round #328 (Div. 2) 592 B. The Monster and the Squirrel
- Priority Queue Implementation
- 4.9 Routing -- Query Parameters
- MiUI手机疯狂输出logcat处理