Hdoj 1242 Rescue

Source: Internet
Author: User

RescueTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21713 Accepted Submission (s): 7748


Problem Descriptionangel was caught by the moligpy! He was put into 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. Their task Is:approach Angel. We assume that "approach Angel" are to get to the position where Angel stays. When there's a guard in the grid, we must kill him (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 we are strong enough to kill all the guards.

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

Inputfirst line contains-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 the Angel ' s friend.

Process to the end of the file.

Outputfor 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 have to stay in the prison all he life."

Sample Input
7 8#.#####.#.a#. r.#. #x ..... #.##...##...#..............

Sample Output
13 Test instructions: "#" Wall, "a" Princess, "R" Reinforcements, "X" guard soldiers, walk no step a second, defeat soldiers one second: Rescue Princess Code:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define INF 0xfffffffusing namespace std, #include <queue> #define MX 500char map[mx][mx];int visit[mx][mx];int N,m,bx,by,ex, Ey,ans;int dx[4]={1,0,0,-1};int dy[4]={0,1,-1,0};struct line{int x,y,step;};    void Getmap ()//take diagram {int i,j;     for (i=0;i<n;i++) {GetChar ();          for (j=0;j<m;j++) {scanf ("%c", &map[i][j]);          if (map[i][j]== ' # ') visit[i][j]=1;              else if (map[i][j]== ' R ') {bx=i;          By=j;        } else if (map[i][j]== ' a ') {ex=i;ey=j;      } else if (map[i][j]== ' x ') visit[i][j]=2;    }}}void BFS () {int i;    queue<line>q;    Line a,temp;    A.x =BX;    A.Y =by;    A.step = 0;    Visit[bx][by]=1;    Q.push (a); while (!        Q.empty ()) {A=q.front ();        Q.pop (); for (i=0;i<4;i++) {temp.x =a.x +dx[i];              Temp.y =a.y +dy[i];              if (visit[temp.x][temp.y]==2) temp.step =a.step +2;             else Temp.step =a.step +1; if (temp.x >=0&&temp.x <n&&temp.y >=0&&temp.y <m&&visit[temp.x][temp.y] !=1) {if (temp.x==ex&&temp.y ==ey) {if (Ans>tem                P.step) Ans=temp.step;                } visit[temp.x][temp.y]=1;             Q.push (temp); }}}}int main () {while (scanf ("%d%d", &n,&m)!=eof) {memset (visit,0,sizeof (v         isit));             Getmap ();         Ans=inf;         DFS ();         if (ANS==0XFFFFFFF) printf ("Poor ANGEL have to stay in the prison all he life.\n");    else printf ("%d\n", ans); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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