Zoj 1649 rescue

Source: Internet
Author: User

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, 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 shocould output a single integer, standing for the minimal time needed. if such a number does no exist, you showould 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

 

Question:

Saving angel

Find the shortest time to reach the angel, kill a guard 1 units time, and take a grid 1 unit time. So, killing a guard to reach the grid 2 units time. Angel has more than one friend.

Solution thinking:

BFS Deformation

Because if the BFS is at an equal distance, the time value in the queue is arranged from small to large, and the priority queue can be directly used.

 

 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         else86             printf("Poor ANGEL has to stay in the prison all his life.\n");87     }88     return 0;89 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.