(2016弱小联盟十一专场10.3) Help the Princess! BFS

题目连接:
https://acm.bnu.edu.cn/v3/statments/jag2016.pdf

分析:

直接判断‘%’到‘@’和最近的‘$’的距离,如果 ‘%’到‘@’小于%’到最近的‘$’的距离则输出YES否则输出NO

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;

const int maxn = 210;
char mmap[maxn][maxn];
int h,w;
int ansp,anss;
int dir[2][4] = {0,0,1,-1,1,-1,0,0};
bool vis[maxn][maxn];

struct node
{
    int x,y,step;
};

void bfs(int x,int y)
{
    memset(vis,false,sizeof(vis));
    queue<node> q;
    node now,ne;
    now.x = x; now.y = y; now.step = 0;
    q.push(now);
    vis[x][y] = true;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            ne.x = now.x + dir[0][i];
            ne.y = now.y + dir[1][i];
            if(!vis[ne.x][ne.y] && ne.x >= 1 && ne.x <= h && ne.y >= 1 && ne.y <= w && mmap[ne.x][ne.y]!='#')
            {
                ne.step = now.step + 1;
                vis[ne.x][ne.y] = true;
                if(mmap[ne.x][ne.y] == '@')
                    ansp = min(ne.step,ansp);
                else if(mmap[ne.x][ne.y] == '$')
                    anss = min(ne.step,anss);
                q.push(ne);
            }
        }
        if(ansp < 9999999 && anss < 9999999) break;
    }

}
int main()
{
    while(scanf("%d%d",&h,&w)!=EOF)
    {
        int x,y;
        ansp = anss = 9999999;
        getchar();
        for(int i=1;i<=h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                scanf("%c",&mmap[i][j]);
                if(mmap[i][j] == '%')
                {
                    x = i; y = j;
                }
            }
            getchar();
        }
        bfs(x,y);
        if(ansp < anss) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值