HDU 1242 BFS+优先队列

 直接贴代码啦!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
#define Pn printf("\n")
#define CSH(a, b) memset(a, (b), sizeof(a))
#define LL long long
using namespace std;
char maze[202][202];
struct node
{
    int x;
    int y;
    int num;
      bool operator < (const node &e) const
    {
       return num>e.num;
    }

}a,b;
int n,m,ax,ay;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

int bfs()
{priority_queue<node> q ;

    q.push(a);
   // a.x=ax;a.y=ay;a.num=0;
    while(!q.empty())
    {
    a=q.top();
    q.pop();
  //  if(maze[a.x][a.y]=='r')
        //return a.num;
    for(int i=0;i<4;i++)
    {
        b.x=a.x+dx[i];
        b.y=a.y+dy[i];
        if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&maze[b.x][b.y]!='#')
        {
            if(maze[b.x][b.y]=='r')
            {
                b.num=a.num+1;
               return b.num;
            }
           else if(maze[b.x][b.y]=='.')
            {
                b.num=a.num+1;
                //q.push(b);

            }
           else if(maze[b.x][b.y]=='x')
            {
                b.num=a.num+2;
               // q.push(b);
            }
            maze[b.x][b.y]='#';
            q.push(b);

        }
    }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",maze[i]);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(maze[i][j]=='a')
                {
                    a.x=i;
                    a.y=j;
                    a.num=0;
                    maze[i][j]='#';
                }
            }
        }
       int t=bfs();
       if(t>0)
       printf("%d\n",t);
       else
        printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值