HDU 2612 Find a way (两次BFS)

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.

Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.

Input The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF

Output For each test case output the minimum total time that both yifenfei andMerceki to
arrival one of KFC.
You may sure there is always have a KFC that can let them meet.

Sample Input

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output

66
88
66

两次BFS即可 需要用数组分别记录Y和M的搜索结果

不是搜到终点就结束  要把图全部搜完

有的肯德基不能到达  step为0不算在答案之中


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<iostream>
#define ll long long
#define ull unsigned long long
const int N=1e9+7;
const int M=205;
using namespace std;

char a[M][M];   //存放地图
int b[4][2]= {1,0,-1,0,0,1,0,-1}; // 方向数组
int c[M][M],d[M][M],e[M][M],w[M][M]; // 标记数组
int n,m,i,j,k;

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

bool check(int x,int y)
{
    return x>=0&&y>=0&&x<n&&y<m&&!w[x][y]&&a[x][y]!='#';
}
void bfs(node p)
{
    memset(w,0,sizeof(w));
    memset(c,0,sizeof(c));
    int x1,y1;
    node f,g;
    queue<node>q;
    q.push(p);
    w[p.x][p.y]=1;
    while(!q.empty())
    {
        f=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            x1=f.x+b[i][0];
            y1=f.y+b[i][1];
            if(check(x1,y1))
            {
                g.x=x1;
                g.y=y1;
                g.step=f.step+1;
                c[x1][y1]=g.step;
                w[x1][y1]=1;
                q.push(g);
            }
        }
    }
}

void init()
{
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
    memset(e,0,sizeof(e));
    memset(w,0,sizeof(w));
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        node p1,p2;
        for(i=0; i<n; i++)
        {
            scanf("%s",a[i]);
            for(j=0; j<m; j++)
            {
                if(a[i][j]=='Y')
                {
                    p1.x=i,p1.y=j,p1.step=0;
                }
                if(a[i][j]=='M')
                {
                    p2.x=i,p2.y=j,p2.step=0;
                }
            }
        }
        bfs(p1);
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                d[i][j]=c[i][j];
            }
        }
        bfs(p2);
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                e[i][j]=c[i][j];
            }
        }
         int ans=N;
         for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(a[i][j]=='@'&&d[i][j]!=0&&e[i][j]!=0)
                {
                    ans=min(ans,d[i][j]+e[i][j]);
                }
            }
        }
        printf("%d\n",ans*11);
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值