Rescue(BFS)

Rescue

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 4
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

Author
CHEN, Xue
题目大意:输入N行M列的字符串
          a代表所要查找的目标,r为起点,每移动一步则耗时1,遇到x耗时2,#为不可移动点
          问找到a时所用最少时间
  

题解:bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r’即为所求

需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就并不能保证每次出队的是时间最小的元素,所以要用优先队列

优先队列(priority_queue)的基本操作:

empty(); 队列为空返回1

pop(); 删除队首值

push(); 将元素归入

top(); 返回队列中优先级最高的元素

size(); 返回队列中元素的个数

具体见:http://blog.csdn.net/flqbestboy/article/details/7830782


#include<cstdio>
#include<queue>
using namespace std;
char map[210][210];//路线输入
bool mark[210][210];//标记此节点是否走过
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//上下左右移动
int m,n,i,j,res;
struct node
{
    int x,y,t;
    friend bool operator<(const node &n1,const node &n2)
    {
        return n1.t>n2.t;
    }//operator重载排序使,排序从大到小,队首值为最小值
}a,p,q;
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        for(i=0;i<m;i++)
         scanf("%s",map[i]);
        for(i=0;i<m;i++)
         for(j=0;j<n;j++)
          if(map[i][j]=='a')
           {
              a.x=i,a.y=j,a.t=0;
              break;
           }
        res=-1,memset(mark,true,sizeof(mark)),mark[a.x][a.y]=false;//数据初始化
        priority_queue<node>Q;
        Q.push(a);
        while(!Q.empty())
        {
            q=Q.top(),Q.pop();
            if(map[q.x][q.y]=='r')
            {
                res=q.t;
                break;
            }
            for(i=0;i<4;i++)
            {
                p.x=q.x+move[i][0],p.y=q.y+move[i][1];
                if(p.x>=0&&p.x<m&&p.y>=0&&p.y<n&&map[p.x][p.y]!='#'&&mark[p.x][p.y])
                {
                   mark[p.x][p.y]=false;
                   if(map[p.x][p.y]=='x')
                     p.t=q.t+2;
                   else
                     p.t=q.t+1;
                   Q.push(p);
                }

            }
        }
        if(res!=-1)//这一步返回时没考虑0,调试了半天....
          printf("%d\n",res);
        else
          printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值