Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21483 Accepted Submission(s): 7660
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.)
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.
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
AC代码及题解:
/*
author: tpbluesky
time: 2015年8月10日18:13:49
题解: 广搜加优先队列,此题有两个坑点,要用优先队列是因为遇到x时时间+2,会破坏队列里元素的有序性
1.angle的朋友可能不止一个,故可以从angle开始广搜
2.angle可能不存在,所以最后还得判断一下
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#define inf 0x3f3f3f3f
#define eps 1e-8
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
const int maxn = 205;
int n, m, vis[maxn][maxn], ans;
char mp[maxn][maxn];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};
struct node
{
int x, y ,step;
int operator<(const node &n) const
{
return step>n.step;
}
};
bool isok(int x,int y)
{
if(x <= 0 || y <= 0 || x > n || y > m || mp[x][y] == '#' || vis[x][y])
return false;
return true;
}
int bfs(int x,int y)
{
node s;
memset(vis,0,sizeof(vis));
s.x = x, s.y = y,s.step= 0;
priority_queue<node> pq;
pq.push(s);
vis[x][y] = 1;
while(!pq.empty())
{
node tp = pq.top();
pq.pop();
if(mp[tp.x][tp.y] == 'r')
{
return tp.step;
}
for(int i = 0;i < 4;++i)
{
node temp;
temp.x = tp.x+dx[i], temp.y = tp.y+dy[i], temp.step = tp.step+1;
if(isok(temp.x,temp.y))
{
vis[temp.x][temp.y] = 1;
if(mp[temp.x][temp.y] == 'x')
temp.step++;
pq.push(temp);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m) == 2)
{
int flag = 1, ans = inf;
for(int i = 1;i <= n;++i)
scanf("%s",mp[i]+1);
for(int i = 1;i <= n;++i)
for(int j = 1;j <= m;++j)
if(mp[i][j] == 'a')
{
int ans = bfs(i,j);
flag = 0;
printf(ans == -1?"Poor ANGEL has to stay in the prison all his life.\n":"%d\n",ans);
i = n+1;
break;
}
if(flag)
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}