G - G hdu Find a way

G - G hdu 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. Sample Input

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

 

 

  题意:两个好朋友(Y和M)要到KFC(@)聚会,求两个人到同一家KFC的最短时间。

 

思路:广搜,存入两个人到同一家KFC的距离。最后找到最短距离。

 

代码:

#include<queue>
#include<cstdio>
#include<string>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int INF=99999999;
char tu[205][205];
int ne[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
int book[205][205];   //标记是否搜过。
int dis[205][205];    //存入Y和M到同一家@的距离。
int n,m,i,j,k;
int Y_x,Y_y,M_x,M_y;   //记下Y和M的位置。
struct zaq
{
    int x,y,step;
};
zaq now,nex;
queue<zaq>qaq;
bool jud(int a,int b)
{
    if(tu[a][b]!='#'&&a>=0&&a<n&&b>=0&&b<m&&!book[a][b])
        return 1;
    return 0;
}
void bfs(int R_x,int R_y)
{
    mem(book,0);
    while(!qaq.empty())
        qaq.pop();
    now.x=R_x;
    now.y=R_y;
    now.step=0;
    qaq.push(now);
    book[now.x][now.y]=1;
    while(!qaq.empty())
    {
        now=qaq.front();
        qaq.pop();
        if(tu[now.x][now.y]=='@')
        {
            if(dis[now.x][now.y]==INF)     //存入Y和M到同一@的距离。
                dis[now.x][now.y]=now.step;
            else
                dis[now.x][now.y]+=now.step;
        }

        for(i=0;i<4;i++)
        {
            nex.x=now.x+ne[i][0];
            nex.y=now.y+ne[i][1];
            if(jud(nex.x,nex.y))
            {
               nex.step=now.step+1;
                book[nex.x][nex.y]=1;
                qaq.push(nex);
            }
        }
    }
}
int main()
{

    while(~scanf("%d%d",&n,&m))
    {
        int ans=INF;        
        for(i=0;i<n;i++)
        {
            scanf("%s",tu[i]);
            for(j=0;j<m;j++)
            {
                if(tu[i][j]=='Y')
                   {
                    Y_x=i;
                    Y_y=j;
                   }
                if(tu[i][j]=='M')
                {
                    M_x=i;
                    M_y=j;
                }
            }
        }
        mem(dis,INF);      //注意,要把dis赋初值为INF
        bfs(Y_x,Y_y);
        bfs(M_x,M_y);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(tu[i][j]=='@'&&ans>dis[i][j]&&book[i][j])  //必须是搜过的@点。参考博客1(见下)
                    ans=dis[i][j]+1;
            }
        }
        printf("%d\n",ans*11);
    }
    return 0;
}

参考博客1:
https://blog.csdn.net/sr_19930829/article/details/18450509

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值