ZOJ 1649 Rescue(BFS)

14 篇文章 0 订阅
3 篇文章 0 订阅
基础BFS
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
const int maxm=205;
const int inf=1<<29;
int m,n;
int x1,y1,x2,y2;
char map[maxm][maxm];
int vis[maxm][maxm];
int w[maxm][maxm];
int dir[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
struct node
{
    int x;
    int y;
    int step;
    int time;
    bool operator <(const node s)const
    {
        return time>s.time;
    }
};
void bfs()
{
    struct node now,pre;
    priority_queue<node>q;
    pre.x=x1;
    pre.y=y1;
    pre.step=0;
    pre.time=0;
    q.push(pre);
    while(!q.empty())
    {
        pre=q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            now.x=pre.x+dir[i][0];
            now.y=pre.y+dir[i][1];
            now.step=pre.step+1;
            now.time=pre.time+1;
            if(now.x>=0&&now.x<m&&now.y>=0&&now.y<n&&map[now.x][now.y]!='#')
            {
                if(map[now.x][now.y]=='x')
                {
                    now.time++;
                }
                if(w[now.x][now.y]>now.time)
                {
                    w[now.x][now.y]=now.time;
                    q.push(now);
                }
            }
        }
    }
    return;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        for(int i=0; i<m; i++)
        {
            scanf("%s",map[i]);
            for(int j=0; j<n; j++)
            {
                w[i][j]=inf;
                if(map[i][j]=='a')
                {
                    x1=i;
                    y1=j;
                }
                if(map[i][j]=='r')
                {
                    x2=i;
                    y2=j;
                }
            }
        }
        w[x1][y1]=0;
        bfs();
        if(w[x2][y2]==inf)
        {
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        }
        else
        {
            printf("%d\n",w[x2][y2]);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值