HDU2612 Find a way

本文介绍了一道名为HDU2612Findaway的问题解决思路及实现代码。该问题要求找到两人从不同起点出发,在地图上到达同一个KFC所需的最短时间。通过两次广度优先搜索(BFS)分别计算每个人到每个点的距离,并遍历所有KFC计算总距离。

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

HDU2612 Find a way

题目描述
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.

这里写图片描述

大致题意
给你一个图,‘.’表示通路,‘#’表示路障,‘@’表示kfc,‘Y’和‘M’表示两个人的起点,
问你这两个人到一个kfc里面碰面所需花费的最小代价,每走一步需要花费11minutes.

思路
分别对两个人进行bfs,计算他们到每个点所用的步数,然后遍历全图,到每个kfc所花的时间就是两个人到该点所花步数之和再乘11,然后将花费最少的kfc的点的值输出即可。

代码如下

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<sstream>
#include<ctime>
using namespace std;  
int dix[4]={0,1,0,-1};
int diy[4]={1,0,-1,0};
int n,m,flag,xy,yy,xm,ym;  
struct node
{
    int x;
    int y;
    int step;
};
queue<node> que;
int f1[205][205],f2[205][205]; //记录两个人到各点所花的步数
char map[205][205];
void bfs1(int x,int y)
{
    node u;
    u.x=x;
    u.y=y;
    u.step=0;
    que.push(u);
    f1[x][y]=-1;
    while(!que.empty())
    {
        node u=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int tx=u.x+dix[i];
            int ty=u.y+diy[i];

            if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&map[tx][ty]!='#'&&f1[tx][ty]==0)
            {

                node v;
                v.x=tx;
                v.y=ty;
                v.step=u.step+1;
                que.push(v);
                f1[tx][ty]=v.step;
            }
        }
    }
}
void bfs2(int x,int y)
{
    node u;
    u.x=x;
    u.y=y;
    u.step=0;
    que.push(u);
    f2[x][y]=-1;
    while(!que.empty())
    {
        node u=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int tx=u.x+dix[i];
            int ty=u.y+diy[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&map[tx][ty]!='#'&&f2[tx][ty]==0)
            {
                node v;
                v.x=tx;
                v.y=ty;
                v.step=u.step+1;
                que.push(v);
                f2[tx][ty]=v.step;
            }
        }
    }
}
int main()  
{  
    while(cin>>n>>m)
    {

        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>map[i][j];
            if(map[i][j]=='Y')
            {
                xy=i;
                yy=j;
            }
            if(map[i][j]=='M')
            {
                xm=i;
                ym=j;
            }
        }
        while(!que.empty()) //初始化
        que.pop();
        memset(f1,0,sizeof(f1));
        memset(f2,0,sizeof(f2));
        bfs1(xy,yy); //对第一个人bfs
        bfs2(xm,ym); //对第二个人bfs
        int maxn=40005;
        for(int i=1;i<=n;i++) //求最少花费
        for(int j=1;j<=m;j++)
        {
            if(map[i][j]=='@'&&f1[i][j]!=0&&f2[i][j]!=0)
            {
               if(maxn>f1[i][j]+f2[i][j])
               {
                 maxn=f1[i][j]+f2[i][j];
               }
            }
        }
        cout<<maxn*11<<endl;
    }
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值