题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15880 Accepted Submission(s): 5756
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
刚开始一直在超时,原因是输入哪里没有加一个~这个符号,弄了好久,=.=,好无语
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
int m,n;
char mp[220][220];
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
typedef struct node
{
int x,y, step;
};
bool operator < (const node &p,const node &q)
{
return p.step > q.step;
}
int judge(int x,int y)
{
if(x<0 || y<0 ||x>=m ||y>=n || mp[x][y]=='#')
return 0;
return 1;
}
int bfs(int row,int col)
{
priority_queue <node> qu;
node s,out;
s.x = row;
s.y = col;
s.step = 0;
qu.push(s);
mp[row][col]='#';
while(!qu.empty())
{
out = qu.top();
qu.pop();
// printf("x= %d,y= %d ,step = %d %c\n",out.x,out.y,out.step,mp[out.x][out.y]);
for(int i=0; i<4; i++)
{
int x = out.x+dir[i][0];
int y = out.y+dir[i][1];
if(judge(x,y))
{
if(mp[x][y] == 'a')
{
printf("%d\n",out.step+1);
return 1;
}
s.x = x;
s.y = y;
s.step = out.step+1;
if(mp[x][y]=='x')
s.step++;
qu.push(s);
mp[x][y]='#';
}
}
}
return 0;
}
/*
7 8
#.#####.
#..#..r.
#.a#x...
..#..#.#
#...##..
.#......
........
*/
int main()
{
while(~scanf("%d %d",&m,&n))
{
int c,d;
int i,j;
for(i=0; i<m; i++)
{
scanf("%s",mp[i]);
for(j=0; j<n; j++)
{
if(mp[i][j]=='r')
{
c=i;
d=j;
}
}
getchar();
}
// printf("mp[1][2]=%c\n",mp[1][2]);
int flag = bfs(c,d);
if(flag==0)
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
//http://acm.hdu.edu.cn/showproblem.php?pid=1242