HDU 1242 Rescue

http://acm.hdu.edu.cn/showproblem.php?pid=1242
题意有一个信息,,天使的朋友可能有多个,即’r’有多个,而’a’只有一个,,所以搜索时,从天使的位置搜索。再用广搜就可以了。

#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cstring>

using namespace std;

struct node
{
    int nx,ny,step;
} info;
int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
int n,m;
int visit[210][210];
char map[210][210];
int bfs(int x,int y)
{
    info.nx=x;
    info.ny=y;
    info.step=0;
    queue<node> q;
    q.push(info);
    while(!q.empty())
    {
        node temp=q.front();
        q.pop();
        //printf("%d,%d,%d\n",temp.nx,temp.ny,temp.step);
        if(map[temp.nx][temp.ny]=='r')
        {
            return temp.step;
        }
        for(int i=0; i<4; i++)
        {
            int xx=temp.nx+dir[i][0];
            int yy=temp.ny+dir[i][1];
            if(xx>=0&&yy>=0&&xx<n&&yy<m&&visit[xx][yy]==0&&map[xx][yy]!='#')
            {
                    info.nx=xx;
                    info.ny=yy;
                    if(map[xx][yy]!='x'){
                        info.step=temp.step+1;
                    }else{
                        info.step=temp.step+2;
                    }
                    //printf("step=%d i=%d xx=%d yy=%d \n",info.step,i,xx,yy);
                    visit[xx][yy]=1;
                    q.push(info);
            }

        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        getchar();
        memset(visit,0,sizeof(visit));
        int ex,ey;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='a')
                {
                    ex=i;
                    ey=j;
                }
            }
            getchar();
        }
        //printf("sx=%d sy=%d ex=%d ey=%d\n",sx,sy,ex,ey);
        visit[ex][ey]=1;
        int a=bfs(ex,ey);

        if(a>0)
        {
            printf("%d\n",a);
        }
        else
        {
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值