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.

InputThe 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

OutputFor 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 主要思想:不能直接用枚举,这样容易超时(不信可以试试)。用bfs把每一个位置遍历,发现@就记录一下。其中也有一些问题,比如怎么对每一个@进行区别,和如果有一个@两个人都不能到达或者有一个人不能到达怎么办(这个需要读题仔细)。
解决方法:用二维数组,对@位置进行记录,再用一个一维数组判断是不是两个人都到了。
#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 10000
using namespace std;
typedef pair<int,int> ee;
bool vis[209][209];
int t[4][2]={0,1,1,0,-1,0,0,-1};
int d[209][209];
char a[209][209];
int b[1009][1009];
int g[1009][1009];
queue<ee> que;
int n,m,k;
int tx,ty,sx,sy,mx,my;
void clean()
{
    while(!que.empty()) que.pop();
    return;
}
void bfs(int x,int y)
{
    memset(d,0,sizeof(d));
    memset(vis,0,sizeof(vis));
    d[x][y]=0;
    que.push(ee(x,y));
    vis[x][y]=true;
    while(!que.empty())
    {
        ee exa=que.front();
        que.pop();
        if(a[exa.first][exa.second]=='@')
        {
            b[exa.first][exa.second]=b[exa.first][exa.second]+d[exa.first][exa.second];
            g[exa.first][exa.second]++;
        }
        for(int i=0;i<4;i++)
        {
            tx=exa.first+t[i][0];
            ty=exa.second+t[i][1];
            if(tx<1||ty<1||tx>n||ty>m) continue;
            if(a[tx][ty]=='#') continue;
            if(vis[tx][ty]) continue;
            que.push(ee(tx,ty));
            vis[tx][ty]=true;
            d[tx][ty]=d[exa.first][exa.second]+1;
//            printf("%d\n",d[tx][ty]);
        }
    }
    clean();
    return;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        k=0;
        memset(b,INF,sizeof(b));
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
        {
        scanf("%s",a[i]+1);
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]=='Y'){sx=i;sy=j;}
            if(a[i][j]=='M'){mx=i;my=j;}
            if(a[i][j]=='@'){b[i][j]=0;}
        }
        }
        bfs(sx,sy);
        bfs(mx,my);
        int x1=1,x2=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(b[i][j]<b[x1][x2]&&g[i][j]==2)
                {
                    b[x1][x2]=b[i][j];
                }
            }
        }
        printf("%d\n",b[x1][x2]*11);
    }
    return 0;
}

随机推荐

  1. TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()

    Variable tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别 使用tf.Variable时,如果检测到命 ...

  2. PHP四大基本排序算法实例

    PHP四大基本排序算法包括:冒泡排序法,快速排序法,选择排序法,插入排序法. 1. 冒泡排序 思路分析:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的数往 ...

  3. golang map输出排序

    由于GoLang Map 内部存储是无序的,当需要按顺序获得map存储的key -value值时,应该对遍历出来的结果进行重新排序: 在go 1.8版本后,提供的slice sort 功能使排序更简单 ...

  4. Everything(一款用于检索硬盘文件的工具)

    有时候文件夹一多,找不到文件,忘记放哪个盘符怎么办? Everything就能帮你解决,比电脑自带的快多啦,官网在此:http://www.voidtools.com/ (也不大,就几M,没有特别的安 ...

  5. layui 文字滚动

    将消息标题滚动 上面是效果 <li class="layui-nav-item"> <div class="layui-carousel" i ...

  6. mysql查询语句,通过limit来限制查询的行数。

    mysql查询语句,通过limit来限制查询的行数. 例如: select name from usertb where age > 20 limit 0, 1; //限制从第一条开始,显示1条 ...

  7. Java基础——Oracle(七)

    一.概述 pl/sql (procedural lanaguage/sql)是 oracle 在标准 sql 上的扩展 .不仅允许嵌入sql 语言,还可以定义变量和常量,允许使用条件语句和循环语句,允 ...

  8. Java基础——Oracle(三)

    一.Oracle内部结构 1.表空间 表空间是数据库的逻辑划分,一个表空间只属于一个数据库,每个表空间由一个或多个数据文件组成,表空间中其他的逻辑结构的数据存储在这些数据文件中,一般oracle系统安 ...

  9. C#特性之数据类型

    这篇文章主要通过演示类在不同发展中的不通过定义方法,来向读者表述它们之间的区别和联系. 在C#1时代,我们喜欢这样定义类: public class Product { private string ...

  10. Nodejs 和 Electron ubuntu下快速安装

    查找时间管理软件的时候发现了superProductivity这个程序,使用electron进行开发,于是看了一下介绍,手痒了,尝试进行环境搭建,下一步慢慢补齐前端知识吧 nodejs安装 nodej ...