算法:广搜;

Description

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

啥也不说了直接代码

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 10000000
int n,m,c[205][205],g[4][2]={-1,0,1,0,0,-1,0,1},e[205][205],a[205][205],b[205][205];
char ch[205][205];
struct dot
{
int x, y;
} ;
void bfs(int dx,int dy,int k)
{ memset(e,0,sizeof(e));
queue<dot>que;
dot cur,loer;
cur.x=dx;cur.y=dy;
if(k==0)a[dx][dy]=0;
else b[dx][dy]=0;
que.push(cur);
while(que.size())
{
loer=que.front();
que.pop();
for(int i=0;i<4;i++)
{
int nx=loer.x+g[i][0];
int ny=loer.y+g[i][1];
if(nx>=0&&nx<n&&ny>=0&&ny<m&&ch[nx][ny]!='#'&&!e[nx][ny])
{
if(k==0)a[nx][ny]=a[loer.x][loer.y]+1;
else if(k==1)b[nx][ny]=b[loer.x][loer.y]+1;
cur.x=nx;
cur.y=ny;
e[nx][ny]=1;
que.push(cur);
}
}
}
}
int main()
{
int i,j,k,p,q,s,t;
while(cin>>n>>m)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>ch[i][j];
a[i][j]=b[i][j]=INF;
if(ch[i][j]=='Y')
{
s=i;t=j;ch[i][j]='#';
}
else if(ch[i][j]=='M')
{
p=i;q=j;ch[i][j]='#';
}
}
}
bfs(s,t,0);
bfs(p,q,1);
int Mx=10000000;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ch[i][j]=='@')
{
if(Mx>a[i][j]+b[i][j])
Mx=a[i][j]+b[i][j];
}
}
}
cout<<11*Mx<<endl;
}
return 0;
}

hdu Find a way的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

  10. HDU 2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:求最近祖先节点的权值和 思路:LCA Tarjan算法 #include <stdio.h&g ...

随机推荐

  1. css3之background

    background background: (1)url(image1.png) right bottom, (2)url(image2.png) center, (3)url(image3.png ...

  2. 关于qq创始人----马化腾的一些琐事

    马化腾(pony)写代码的水平如何? 一位产品经理吐槽: 曾经和pony一起写过代码. 当时5个人挤在一个只有四个位置的房间里,埋头开发,用C++.我当时负责写一个通讯模块,有一个bug弄了两天,没有 ...

  3. C语言基础学习基本数据类型-其他整数类型

    其他整数类型 初学C语言时,int类型会满足你对整数的大多数需求. C语言还提供了三个关键字用以修饰基本的整数类型:short.long和unsigned.有以下几个注意点: (1)C语言没有具体规定 ...

  4. HTML&CSS基础学习笔记1.18-表格的边框

    今天的内容比较简单~来看看HTML里表格的边框是怎么设置的吧 表格的边框 前面为了方便观察表格单元格的情况,我们给<table>加了border属性.<table>的borde ...

  5. Python之简单工厂模式实现

    最近又看了下大话设计模式,决定用Python来试着实现下. 基础类 class OperationBase(): """ 基础运算类 """ ...

  6. Ubuntu-Java-Scala-Spark-IEDA-configure

    最近要接触数据分析,需要快速入门,就想在Ubuntu下配置IDEA和Spark编程环境. 1.下载jdk #java /etc/profile .zshrc, 或者直接在终端输入export JAVA ...

  7. QQ截图时窗口自动识别的原理(WindowFromPoint, ChildWindowFromPoint, ChildWindowFromPointEx,RealChildWindowFromPoint)

    新版的QQ在截图时加入了窗口自动识别的功能,能根据鼠标的位置自动画出下面窗口的轮廓.今天有人在论坛上问起这个问题,下面我们来探讨这个功能的实现原理. 首先我们要明白截图软件的基本原理,截图时实际上是新 ...

  8. 在WPF中使用AForge.net控制摄像头拍照

    原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须 ...

  9. ASP.NET WEB API 如何使用基于Post的方式传递多个值(二)

    前面我曾经写过一篇文章,是基于HttpContext的请求上下文中读取表单参数,其实还可以将其单独拆分出来. 基于Filter的方式 获取表单值:(核心代码)   public void OnActi ...

  10. iptables 顺序

    -A INPUT -s 115.236.6.6/32 -p udp -m udp --dport 111 -j ACCEPT -A INPUT -s 10.175.197.98/32 -p udp - ...