Find a way(bfs专题)

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.
翻译结果(有误,可结合原题面分析):
终于在杭州度过了一年的学习,一飞回到家乡宁波。离开宁波一年了,亦芬菲有很多人要见面。特别是一个好朋友梅尔斯基。

一奋飞的家在乡下,梅尔斯基的家在市中心。于是一奋飞和奔驰安排在肯德基见面。在宁波有很多肯德基,他们想选择一个让总时间最小的。

现在给你一张宁波地图,奔驰和易芬飞都可以上下左右移动到相邻的路上,只要11分钟。

输入

输入包含多个测试用例。

每个测试用例包括,前两个整数n,m (2<=n,m<=200)。

接下来n行,每一行包含m个字符。

'Y’表示yifenfei的初始位置。

'M’表示Merceki的初始位置。

'#'表示不可通行;

'.'表示道路。

'@表示’KCF

输出

对于每个测试用例输出的最短总时间,即M和Y 到达肯德基的时间。你可以肯定,总有一个肯德基,可以让他们满足。

样例输入:
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

样例输出:
66
88
66

解题思路:
首先找到Y到每个KFC(@)的最短距离,并分别存放
,再找到m到每个KFC(@)的最短距离,也分别存放。
最后将Y,M到对应KFC的距离加起来,找到最小值即可。
代码如下:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<stdlib.h>
using namespace std;
int n,m,h1,h2,r1,r2;
struct  node
{
    int x,y,step;
};
int mov[4][2]= {0,1,0,-1,1,0,-1,0};
struct kfc
{
    int x,y,step=0;
} k[205][205];
struct kfc2
{
    int x,y,step=0;
} k2[205][205]; char p[205][205];
        int bj[205][205];
struct person
{
    int x,y,step;
} reny1,reny2,renm1,renm2;
queue<person> ppp;
int main ()
{
    while(~scanf("%d %d",&n,&m))
    {
        int minn =400000,sum;
        getchar();
       memset(bj,0,sizeof(bj));
        for(int i=0; i<204; i++)
        {
            for(int j=0; j<204; j++)
            {
                k[i][j].step=0;
                k2[i][j].step=0;
            }
        }


        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
                scanf("%c",&p[i][j]);
            getchar();
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(p[i][j]=='Y')	//找到Y的坐标并保存
                {
                    reny1.x=i;
                    reny1.y=j;
                }
                if(p[i][j]=='M')   //找到M的坐标并保存
                {
                    renm1.x=i;
                    renm1.y=j;
                }
            }
        }
        //遍历Y到每个KFC的距离并保存
        reny1.step=0;
        ppp.push(reny1);
        while(!ppp.empty())
        {
            reny1=ppp.front();
            ppp.pop();
            if(p[reny1.x][reny1.y]=='@')
            {

                k[reny1.x][reny1.y].step=reny1.step;
            }
            reny2.step=reny1.step+1;
            for(int i=0; i<4; i++)
            {
                reny2.x=reny1.x+mov[i][0];
                reny2.y=reny1.y+mov[i][1];
                if(reny2.x<0||reny2.y<0||reny2.x>=n||reny2.y>=m||bj[reny2.x][reny2.y]||p[reny2.x][reny2.y]=='#')
                    continue;
                bj[reny2.x][reny2.y]=1;
                ppp.push(reny2);
            }
        }
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                bj[i][j]=0;
                //遍历M到每个KFC的距离并保存
        renm1.step=0;
        ppp.push(renm1);
        while(!ppp.empty())
        {
            renm1=ppp.front();
            ppp.pop();
            if(p[renm1.x][renm1.y]=='@')
            {
                k2[renm1.x][renm1.y].step=renm1.step;
            }
            renm2.step=renm1.step+1;
            for(int i=0; i<4; i++)
            {
                renm2.x=renm1.x+mov[i][0];
                renm2.y=renm1.y+mov[i][1];
                if(renm2.x<0||renm2.y<0||renm2.x>=n||renm2.y>=m||bj[renm2.x][renm2.y]||p[renm2.x][renm2.y]=='#')
                    continue;
                bj[renm2.x][renm2.y]=1;
                ppp.push(renm2);
            }
        }
        //将Y和M到对应的KFC的最短距离相加并比较,取得最小值
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                sum=k[i][j].step+k2[i][j].step;
                k[i][j].step=0;
                k2[i][j].step=0;
                if(minn>sum&&sum!=0)
                {
                    minn=sum;
                }
            }
        }
        printf("%d\n",minn*11);
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值