Rescue(BFS)

Rescue
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

r到a 每步1秒 遇到x要打死 就用时2秒

#include<cstdio>  
#include<cstring>  
#include<queue>  
using namespace std;  
char map[1010][1010];  
int  vis[1010][1010];  
struct node  
{  
    int hang ,lie,step;  
    friend bool operator<(node x,node y)//优先队列 遇到.和x先走.时间比较短  
    {  
        return x.step >y.step ;  
    }  
};  
int main()  
{  
    int n,m;  
    while(scanf("%d%d",&n,&m)!=EOF)  
    {  
        memset(vis,0,sizeof(vis));  
        memset(map,0,sizeof(map));  
            for(int i=0;i<n;i++)  
    {  
        scanf("%s",map[i]);  
    }  
    struct node start;  
    for(int i=0;i<n;i++)  
    {  
        for(int j=0;j<m;j++)  
        {  
            if(map[i][j]=='r')  
            {  
                start.hang =i;  
                start.lie =j;  
                start.step =0;  
                vis[i][j]=1;  
            }  
        }  
    }  
    priority_queue<node>q;  
    q.push(start);  
    int flog=0;  
    while(!q.empty() )   
    {  
        struct node tmp=q.top() ;  
        q.pop() ;  
        if(map[tmp.hang][tmp.lie ]=='a')  
        {  
            flog=1;  
            printf("%d\n",tmp.step );  
            break;  
        }  
        tmp.step ++;  
        struct node tmp2;  
        if(tmp.hang +1<n)  
        {  
            tmp2=tmp;  
            tmp2.hang ++;  
            if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)  
            {  
                    if(map[tmp2.hang ][tmp2.lie ]=='x')  
                    {  
                        tmp2.step ++;  
                        }  
                vis[tmp2.hang ][tmp2.lie ]=1;  
                q.push(tmp2);   
            }  
        }  
            if(tmp.hang -1>=0)  
        {  
            tmp2=tmp;  
            tmp2.hang --;  
            if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)  
            {  
                vis[tmp2.hang ][tmp2.lie ]=1;  
                    if(map[tmp2.hang ][tmp2.lie ]=='x')  
                  {  
                        tmp2.step ++;  
                    }  
                q.push(tmp2);   
  
            }  
        }  
            if(tmp.lie +1<m)  
        {  
            tmp2=tmp;  
            tmp2.lie ++;  
            if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)  
            {  
                vis[tmp2.hang ][tmp2.lie ]=1;  
                    if(map[tmp2.hang ][tmp2.lie ]=='x')  
                    {  
                            tmp2.step ++;  
                     }  
                q.push(tmp2);   
            }  
        }  
            if(tmp.lie -1>=0)  
        {  
            tmp2=tmp;  
            tmp2.lie --;  
            if(map[tmp2.hang ][tmp2.lie ]!='#'&&vis[tmp2.hang ][tmp2.lie ]==0)  
            {  
                vis[tmp2.hang ][tmp2.lie ]=1;  
                        if(map[tmp2.hang ][tmp2.lie ]=='x')  
                            {  
                         tmp2.step ++;  
                            }  
                q.push(tmp2);   
            }  
        }  
    }  
    if(flog==0)  
    {  
        printf("Poor ANGEL has to stay in the prison all his life.\n");  
    }  
    }  
  
    return 0;  
 }


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值