hdoj 1242 Rescue

来源:互联网 发布:dota2数据分析师招聘 编辑:程序博客网 时间:2024/06/08 05:39

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21713    Accepted Submission(s): 7748


Problem 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
题意:
“#”墙,“a”公主,“r”救兵,“x”守卫士兵,走路没步一秒,打败士兵一秒:救兵救公主
代码:
#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()//取图 {    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>temp.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(visit));         Getmap();             ans=INF;         dfs();         if(ans==0xfffffff)         printf("Poor ANGEL has to stay in the prison all his life.\n");         else        printf("%d\n",ans);    }    return 0;}



1 0