HDU 1242 Rescus (Memory search)

Source: Internet
Author: User

Rescue

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 Input7 8 #.#####.#.a#. r.#. #x ..... #.. #.##...##.. .#..............

Sample Output13 Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineINF 999995 using namespacestd;6 7 Chargraph[205][205];8 BOOLvisit[205][205];9 intdp[205][205];Ten intm,n,cnt; One intEx,ey,sx,sy; A intd[4][2]={1,0,0,1,0,-1,-1,0}; -  - voidDfsintXinty) the { -     if(Dp[x][y]>=dp[ex][ey])return; -      for(intI=0;i<4; i++) -     { +         intxx=x+d[i][0]; -         intyy=y+d[i][1]; +         if(xx<0|| xx>=m| | yy<0|| yy>=n| | graph[xx][yy]=='#'|| visit[xx][yy]==false)Continue; A         if(graph[xx][yy]=='x') at         { -             if(dp[xx][yy]==-1|| dp[xx][yy]>dp[x][y]+2) -dp[xx][yy]=dp[x][y]+2; -             Else Continue; -         } -         Else if(graph[xx][yy]=='a') in         { -             if(dp[xx][yy]==-1|| dp[xx][yy]>dp[x][y]+1) todp[xx][yy]=dp[x][y]+1; +             return; -         } the         Else if(graph[xx][yy]=='.') *         { $             if(dp[xx][yy]==-1|| dp[xx][yy]>dp[x][y]+1)Panax Notoginsengdp[xx][yy]=dp[x][y]+1; -             Else Continue; the         } +visit[xx][yy]=false; A DFS (XX,YY); thevisit[xx][yy]=true; +     } - } $  $ intMain () - { - //freopen ("In.txt", "R", stdin); the     inti,j; -      while(SCANF ("%d%d", &m,&n)! =EOF)Wuyi     { theex=ey=-1; -memset (Visit,1,sizeof(visit)); Wu          for(i=0; i<m;i++) -scanf"%s", Graph[i]); About          for(i=0; i<m;i++) $          for(j=0; j<n;j++) -         { -dp[i][j]=-1; -             if(graph[i][j]=='R') Asx=i,sy=J; +             Else if(graph[i][j]=='a') thedp[i][j]=inf,ex=i,ey=J; -             Else Continue; $         } the         if(ex==-1&&ey==-1) the         { theprintf"Poor ANGEL have to stay in the prison all his life.\n"); the             Continue; -         } indp[sx][sy]=0; the DFS (sx,sy); the         if(dp[ex][ey]!=inf) Aboutprintf"%d\n", Dp[ex][ey]); the         Else theprintf"Poor ANGEL have to stay in the prison all his life.\n"); the     } +     return 0; -}

HDU 1242 Rescus (memory search)

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.