ZOJ 1649 Rescue

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.)


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.


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

 

题目大意:

救天使

求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit time 。SO,杀死一个护卫到达那个格子 2units time。Angel的朋友不只有一个,可能有多个

解题思想:

BFS变形

因为如果等距离的BFS的话,队列里的time值是从小往大排的,直接用优先队列就可以

 

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <queue>

 4 #define MAX 201

 5 

 6 using namespace std;

 7 typedef struct ANG{

 8     int t;

 9     int x, y;

10 } ANG;

11 

12 char map[MAX][MAX];

13 int vis[MAX][MAX];

14 int n, m;

15 int dir[8] = {0,1,0,-1,1,0,-1,0};

16 priority_queue <ANG> q;

17 

18 bool operator<(ANG a, ANG b)

19 {

20     return a.t > b.t;

21 }

22 

23 int bfs()

24 {

25     ANG tmp;

26     int i,x,a,b,aa,bb,t;

27     while(!q.empty()){

28         tmp = q.top();

29         q.pop();

30         a = tmp.x;

31         b = tmp.y;

32         t = tmp.t;

33         for(i = 0; i < 8; i += 2){

34             aa = a + dir[i];

35             bb = b + dir[i+1];

36             if(aa >= 0 && aa < n && bb >=0 && b < m && map[aa][bb] != '#' && !vis[aa][bb]){

37                 vis[aa][bb] = 1;

38                 if( map[aa][bb] == 'a' )

39                     return t+1;

40                 if( map[aa][bb] == '.' ){

41                     tmp.t = t + 1;

42                     tmp.x = aa;

43                     tmp.y = bb;

44                     q.push(tmp);

45                 }

46                 if( map[aa][bb] == 'x'  ){

47                     tmp.t = t + 2;

48                     tmp.x = aa;

49                     tmp.y = bb;

50                     q.push(tmp);

51                 }

52             }

53         }

54     }

55     return -1;

56 }

57 

58 int main()

59 {

60     int i, k, ans;

61     ANG tmp;

62     while(~scanf("%d%d", &n, &m)){

63         memset(map, 0, sizeof(map));

64         for(i=0; i<n; i++)

65             scanf("%s", map[i]);

66 

67         while(!q.empty())

68             q.pop();

69         memset(vis, 0, sizeof(vis));

70 

71         for(i = 0; i < n; i++)

72             for(k = 0; k < m; k++)

73                 if(map[i][k] == 'r'){

74                     map[i][k] = '.';

75                     tmp.x = i;

76                     tmp.y = k;

77                     tmp.t = 0;

78                     q.push(tmp);

79                     vis[i][k] = 1;

80                 }

81 

82         ans = bfs();

83         if(ans != -1)

84             printf("%d\n",ans);

85         else

86             printf("Poor ANGEL has to stay in the prison all his life.\n");

87     }

88     return 0;

89 }

 

你可能感兴趣的:(res)