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 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 Out

66

88

66

思路 :两个人各自bfs 一次在每个‘@’里挑一个使他们时间和最小的值。本来很简单的一道题目,被我复杂化了,首先我想不明白应不应该让他们穿过kfc店,后来想了想他们完全可以路过那里啊,不能说,一个人在路边开了个店难道大家还不能走这个路了吗?要说坑点吗,倒是有一个小坑点,就是有些店可能他们去不了,不要影响结果。

#include <cstdio>
#include <queue>
#include <climits>
#include <cstring>
#include <iostream>
using namespace std;
char mp[210][210];
int mk[210][210];
int time[210][210];
int m,n;
typedef struct node{
   int x,y,t;
   node(int x_=0,int y_=0,int t_=0)
   {
       x = x_,y = y_, t = t_;
   }
}node;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
void bfs(int x,int y)
{
    queue <node> q;
    node tmp(x,y,0);
    q.push(tmp);
    mk[x][y] = 1;
    while(!q.empty())
    {
        tmp = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int x_ = tmp.x+dir[i][0];
            int y_ = tmp.y+dir[i][1];
            if(x_<1||y_<1||x_ > m||y_>n) continue;
            if(mp[x_][y_] == '#') continue;
            if(mk[x_][y_]) continue;
            if(mp[x_][y_] == '@')
            {
                time[x_][y_] += tmp.t+1;
            }
            mk[x_][y_] = 1;
            node nd(x_,y_,tmp.t+1);
            q.push(nd);
        }
    }
}
int main()
{
    while(cin >>m>>n)
    {
        node M,Y;
        memset(time,0,sizeof(time));
        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cin>>mp[i][j];
                if(mp[i][j] == 'Y') Y.x = i,Y.y = j;
                if(mp[i][j] == 'M') M.x = i,M.y = j;
            }
        }
        memset(mk,0,sizeof(mk));
        bfs(Y.x,Y.y);
        memset(mk,0,sizeof(mk));
        bfs(M.x,M.y);
        int ans = INT_MAX;
        for(int i = 1; i <= m;i++)
        for(int j = 1; j <= n;j++)
        {
             if(mp[i][j] == '@'&&mk[i][j])  ans = min(ans,time[i][j]); // mk很重要
        }
        printf("%d\n",ans*11);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值