杭电1242---Rescue

来源:互联网 发布:软件操作说明书模版 编辑:程序博客网 时间:2024/09/21 09:00

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 putin 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. Theirtask is: approach Angel. We assume that "approach Angel" is to get tothe position where Angel stays. When there's a guard in the grid, we must killhim (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 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 two 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 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 has to stayin the prison all his life."

Sample Input

7 8

#.#####.

#.a#..r.

#..#x...

..#..#.#

#...##..

.#......

........

Sample Output

13

题意:只是一道简单的BFS问题,遇到卫兵就让时间加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;          //卫兵时间上加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 has to stay in the prison all his life.\n");    }    return 0;}


 

 


0 0
原创粉丝点击