Rescue
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 5895 accepted submission (s): 2192
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 Wils, 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,
Course .)
Input
First line contains two integers stand for N and M.
Then
N lines follows, every line has m characters. "." stands for Road, ""
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 shocould output a single integer, standing
For the minimal time needed. If such a number does no exist, you shoshould
Output A line containing "Poor Angel has to stay in the prison all his
Life ."
Sample input7 8
#.#####.
#. A #... R.
#... # X...
..#..#.#
#...##..
.#......
........
Sample output13 is a simple search question. Here, because the question says that there may be multiple rescuers, the reverse search starts from a, so that you can find the nearest defender, because the time is not strictly + 1 when the question is entered, the nature of BFS is damaged to some extent, so the priority queue is corrected. The Code is as follows:
#include <cstdio>#include <cstdlib>#include <queue>#include <cstring>using namespace std;char map[205][205], hash[205][205];struct Node{int x, y, step;bool operator < ( const Node &t ) const{ return t.step< step;}}info;int sx, sy, dir[4][2]= { 1, 0, -1, 0, 0, 1, 0, -1 };bool BFS( int &ans ){memset( hash, 0, sizeof( hash ) );priority_queue< Node >q;info.x= sx, info.y= sy, info.step= 0;hash[sx][sy]= 1;q.push( info );while( !q.empty() ){Node pos= q.top();q.pop();if( map[ pos.x ][ pos.y ]== 'r' ){ans= pos.step;return true;}for( int i= 0; i< 4; ++i ){int x= pos.x+ dir[i][0], y= pos.y+ dir[i][1], step= pos.step+ 1;if( map[x][y]!= '#'&& map[x][y]!= 0 ){if( ( map[x][y]== '.'|| map[x][y]== 'r' )&& !hash[x][y] ){info.x= x, info.y= y, info.step= step;hash[x][y]= 1;q.push( info );}else if( map[x][y]== 'x'&& !hash[x][y] ){info.x= x, info.y= y, info.step= step+ 1;hash[x][y]= 1;q.push( info );}}}}return false;}int main(){int N, M;while( scanf( "%d %d", &N, &M )!= EOF ){int flag= 0;memset( map, 0, sizeof( map ) );for( int i= 1; i<= N; ++i ){scanf( "%s", map[i]+ 1 );for( int j= 1; j<= M; ++j ){if( !flag&& map[i][j]== 'a' ){sx= i, sy= j;flag= 1;}}}int ans;if( BFS( ans ) ){printf( "%d\n", ans );}else{puts( "Poor ANGEL has to stay in the prison all his life." );}}}