Hangzhou Electric 1242---Rescue

Source: Internet
Author: User

Rescue

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)

Total submission (s): 19478 Accepted Submission (s): 6939

Problem Description

Angel was caught by the moligpy! He was Vladimir Prison by Moligpy. The prison is described as a n * M (n, M <=) matrix. There is WALLs, ROADs, and guards in the prison.

Angel ' s friends want to save Angel. Theirtask Is:approach Angel. We assume that "approach Angel" are to get tothe position where Angel stays. When there's a guard in the grid, we must Killhim (or his?) 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 Weare strong enough to kill all the guards.

You have to calculate the minimal time Toapproach Angel. (We can move only up, down, left and right, to the neighbor Gridwithin bound, of course.)

Input

First line contains, integers stand forn and M.

Then N lines follows, every line has mcharacters. "." stands for road, "a" stands for Angel, and "R" stands for each of the Angel ' s friend.

Process to the end of the file.

Output

For each test case, your program shouldoutput a single integer, standing for the minimal time needed. If such a numberdoes no exist, you should output a line containing "Poor ANGEL have to stayin the prison all of his life."

Sample Input

7 8

#.#####.

#.a#. R.

#.. #x ...

.. #.. #.#

#...##..

.#......

........

Sample Output

13

Test instructions: Just a simple bfs problem, when you meet the guards, let time add 1.

#include <iostream> #include <stdio.h> #include <queue> #include <string.h>using namespace std;    int dx[]={1,0,0,-1};int dy[]={0,1,-1,0};int n,m;int vis[205][205];char mp[205][205];struct dot{int x, y; int time;};    inline bool in (dot gx) {if (gx.x>=0&&gx.x<n&&gx.y>=0&&gx.y<m) return true; return false;}        int main () {while (cin>>n>>m) {dot gx;        queue<dot>q;       while (!q.empty ()) Q.pop ();        memset (vis,0,sizeof (VIS));         for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>>mp[i][j];                    for (int i=0;i<n;i++) for (int j=0;j<m;j++) {if (mp[i][j]== ' R ') {                    Gx.x=i;                    Gx.y=j;                gx.time=0;                } else if (mp[i][j]== ' # ') {vis[i][j]=1; }} Q.PUsh (GX);            Vis[gx.x][gx.y]=1;            int step=0;                while (!q.empty ()) {dot tmp,next;                Tmp=q.front (), Q.pop ();          if (mp[tmp.x][tmp.y]== ' x ') {tmp.time=tmp.time+1;                    Guard time plus 1} for (int i=0;i<4;i++) {next.x=tmp.x+dx[i];                    Next.y=tmp.y+dy[i];                    next.time=tmp.time+1;                        if (in (next) &&!vis[next.x][next.y]) {Q.push (next);                        Vis[next.x][next.y]=1;                            if (mp[next.x][next.y]== ' a ') {step=next.time;                        Break            }}} if (step>0) break;          } if (step>0) printf ("%d\n", step);  else printf ("Poor ANGEL have to stay in the prison all his life.\n"); } return 0;}



Hangzhou Electric 1242---Rescue

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.