Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16592 Accepted Submission(s): 6030
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
Source
Recommend
Eddy | We have carefully selected several similar problems for you:
1016
1072
1175
1548
1180
尼玛输出忘了个句号wa了几发
尼玛输出忘了个句号wa了几发
#include<map>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool vis[205][205];
char mat[205][205];
const int inf = 0x3f3f3f3f;
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int n, m;
struct node
{
int x, y;
int step;
friend bool operator < (const node a, const node b)
{
return a.step > b.step;
}
};
priority_queue<node> qu;
bool is_legal(int x, int y)
{
if( x < 0 || y < 0 || x >= n || y >= m || mat[x][y] == '#')
return false;
return true;
}
int bfs(int sx, int sy, int ex, int ey)
{
while( !qu.empty() )
qu.pop();
memset( vis, 0 ,sizeof(vis) );
node temp1, temp2;
temp1.x = sx;
temp1.y = sy;
temp1.step = 0;
qu.push(temp1);
vis[sx][sy] = 1;
while( !qu.empty() )
{
temp1 = qu.top();
qu.pop();
if(temp1.x == ex && temp1.y == ey)
{
return temp1.step;
}
for(int i = 0; i < 4; i++)
{
temp2.x = temp1.x + dir[i][0];
temp2.y = temp1.y + dir[i][1];
if( !is_legal(temp2.x, temp2.y) )
continue;
if( vis[temp2.x][temp2.y] )
continue;
vis[temp2.x][temp2.y] = 1;
temp2.step = temp1.step + 1;
if(mat[temp2.x][temp2.y] == 'x')
temp2.step++;
qu.push(temp2);
}
}
return inf;
}
int main()
{
while(~scanf("%d%d", &n,&m))
{
int ex, ey;
for(int i = 0; i < n; i++)
{
scanf("%s", mat[i]);
for(int j = 0; j < m; j++)
{
if(mat[i][j] == 'a')
{
ex = i;
ey = j;
break;
}
}
}
int ans = inf;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
if(mat[i][j] == 'r')
{
ans = min(ans, bfs(i, j, ex, ey));
//break;
}
}
if(ans != inf)
printf("%d\n", ans);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}