HDU 2612 Find a way

本文介绍了一种使用广度优先搜索(BFS)算法来解决两个角色寻找最近共享目标的问题。具体场景为两个人物J和M寻找最近的KFC,算法需要计算两者到达任一KFC的最短时间总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 and Merceki 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


题意

J和M想去一个共同的@,求最近他俩到@距离和*11;

思路

bfs,注意的是@和两个起点是可以路过的(我这样做AC了)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <cstring>
using namespace std;
#define inf 0x3f3f3f3f
char a[220][220];
int vis[220][220];
int n,m,i,j;
int x1,x2,yy,y2;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
struct node
{
    int x,y,step;
    int minx,miny;
};
node p[220][220];
int bfs()
{
    queue<node> q;
    node u,v;
    u.x=x1,u.y=yy;
    u.step=0;
    q.push(u);
    memset(vis,0,sizeof(vis));
    vis[x1][yy] = 1;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        if(a[u.x][u.y]=='@')
        {
            p[u.x][u.y].minx = min(p[u.x][u.y].minx,u.step);
        }
        for(i=0;i<4;i++)
        {
            int tx = u.x+dx[i];
            int ty = u.y+dy[i];
            if(tx<1||ty<1||tx>n||ty>m||a[tx][ty]=='#') continue;
            if(!vis[tx][ty])
            {
                vis[tx][ty] = 1;
                v.x = tx,v.y = ty;
                v.step = u.step+1;
                q.push(v);
            }
        }
    }

    u.x=x2,u.y=y2;
    u.step=0;
    q.push(u);
    memset(vis,0,sizeof(vis));
    vis[x2][y2] = 1;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        if(a[u.x][u.y]=='@')
        {
            p[u.x][u.y].miny = min(p[u.x][u.y].miny,u.step);
        }
        for(i=0;i<4;i++)
        {
            int tx = u.x+dx[i];
            int ty = u.y+dy[i];
            if(tx<1||ty<1||tx>n||ty>m||a[tx][ty]=='#') continue;
            if(!vis[tx][ty])
            {
                vis[tx][ty] = 1;
                v.x = tx,v.y = ty;
                v.step = u.step+1;
                q.push(v);
            }
        }
    }
    int ans = inf;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            if(a[i][j]=='@')
            {
                ans = min(ans,p[i][j].minx+p[i][j].miny);
            }
        }
    }
    return ans*11;
}

int main()
{
    while(cin>>n>>m)
    {
        for(i=1;i<=n;i++)
        {
            getchar();
            for(j=1;j<=m;j++)
            {
                scanf("%c",&a[i][j]);
                if(a[i][j]=='Y')
                {
                    x1=i,yy=j;
                }
                if(a[i][j]=='M')
                {
                    x2=i,y2=j;
                }
                if(a[i][j]=='@')
                {
                    p[i][j].minx = inf;
                    p[i][j].miny = inf;
                }
            }
        }
        int ans = bfs();
        cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值