HDU 2612 Find a way

bfs水题。

Y和M 一起出发 到达 @ 的位置。求最近的。

分别以Y和M做一次bfs 即可。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define FOR0(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 200+1
using namespace std;

int xx[]={0,0,-1,1};
int yy[]={-1,1,0,0};

int n,m;
int g[SIZE][SIZE];
int a[SIZE][SIZE];
int b[SIZE][SIZE];

struct lx
{
    int x,y,t;
    void init(int xx,int yy,int tt)
    {
        x=xx,y=yy,t=tt;
    }
}Y,M;

void bfs(lx start,int dis[][SIZE])
{
    bool vis[SIZE][SIZE];
    CLR(vis,0);
    CLR(dis,0);
    vis[start.x][start.y]=1;
    queue<lx>q;
    q.push(start);
    while(!q.empty())
    {
        lx tmp=q.front();
        q.pop();
//        printf("%d %d %c\n",tmp.x,tmp.y,g[tmp.x][tmp.y]);
//        system("pause");
        FOR(k,0,4)
        {
            int x=tmp.x+xx[k];
            int y=tmp.y+yy[k];
            if(x<0||y<0||x>=n||y>=m||g[x][y]=='#'||vis[x][y])continue;
            lx now;
            vis[x][y]=1;
            now.init(x,y,tmp.t+1);
            dis[x][y]=now.t;
            q.push(now);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        FOR(i,0,n)
        {
            char str[SIZE];
            scanf("%s",str);
            FOR(j,0,m)
            {
                g[i][j]=str[j];
                if(str[j]=='Y')
                    Y.init(i,j,0);
                else if(str[j]=='M')
                    M.init(i,j,0);
            }
        }
        bfs(Y,a);
        bfs(M,b);
        int ans=SIZE*SIZE;
        FOR(i,0,n)
        FOR(j,0,m)
        {
            if(g[i][j]!='@')continue;
            ans=min(ans,a[i][j]+b[i][j]);
        }
        printf("%d\n",ans*11);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值