L. 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两人所走总距离最短,(注意KFC可以当做路)。
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#define N 210
#define inf 0xffffff
using namespace std;
int n,m,flag;
int dir[][]={,,,,-,,,-};
char s[N][N];
int mark[N][N],dis[N][N][];
struct node
{
int x,y,step;
};
bool judge(int x,int y)
{
if(x>= && x<m && y>= && y<n && s[x][y]!='#' && mark[x][y]==)
return ;
return ;
}
void bfs(int x,int y)
{
queue<node>q;
node cur,next;
cur.x=x;cur.y=y;cur.step=;
mark[x][y]=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
next.step=cur.step+;
for(int k=;k<;k++)
{
next.x=x=cur.x+dir[k][];
next.y=y=cur.y+dir[k][];
if(judge(x,y))
{
mark[x][y]=;
if(s[x][y]=='@')
dis[x][y][flag]=next.step;
q.push(next);
} }
} }
int main()
{
int i,j,min;
while(scanf("%d%d",&m,&n)!=EOF&&n&&m)
{
min=inf;
for(i=;i<m;i++)
for(j=;j<n;j++)
dis[i][j][]=dis[i][j][]=inf;
for( i=;i<m;i++)
scanf("%s",s[i]);
for(i=;i<m;i++)
for( j=;j<n;j++)
{
if(s[i][j]=='Y')
{
flag=;
memset(mark,,sizeof(mark));
bfs(i,j);
}
else if(s[i][j]=='M')
{
flag=;
memset(mark,,sizeof(mark));
bfs(i,j);
}
}
for(i=;i<m;i++)
for(j=;j<n;j++)
if(s[i][j]=='@' && min>dis[i][j][]+dis[i][j][])
min=dis[i][j][]+dis[i][j][];
printf("%d\n",min*);
}
return ;
}
 

Find a way——L的更多相关文章

  1. JavaWeb 后端 <二> 之 Servlet 学习笔记

    一.Servlet概述 1.什么是Servlet Servlet是一个运行在服务器端的Java小程序,通过HTTP协议用于接收来自客户端请求,并发出响应. 2.Servlet中的方法 public v ...

  2. HTML5 3D 粒子波浪动画特效DEMO演示

    需要thress.js插件:     http://github.com/mrdoob/three.js // three.js - http://github.com/mrdoob/three.js ...

  3. 排序算法----基数排序(RadixSort(L))单链表智能版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  4. VC 中与字符串相关的宏 _T、TEXT,_TEXT、L 的作用

    CSDN原博文:http://blog.csdn.net/houkai363/article/details/8134787 遇到了:不能将参数 1 从“const char [5]”转换为“LPCT ...

  5. 排序算法----基数排序(RadixSort(L,max))单链表版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  6. BZOJ 1212: [HNOI2004]L语言 [AC自动机 DP]

    1212: [HNOI2004]L语言 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1367  Solved: 598[Submit][Status ...

  7. latex均方极限符号l.i.m在lyx下的输入方法

    $\mathop{l.i.m}\limits_{x\to +\infty}$ 命令说明: 1.指定数学环境$$ 2.\mathop{l.i.m}指数学符号自定义为l.i.m 3.\limits_{x\ ...

  8. 求单链表L各结点的阶乘之和(c语言)

    链表需要用到指针 阶乘需要用到递归 链表中的注意事项: 1.链表L是否等于NULL ----------是循环结束的条件 2.链表L->Data ---------取链表L中各个结点的值 3.L ...

  9. 模仿迅L看看<音频播放器> 实现点击进度条,跳转播放

    <Style x:Key="btnFallback" TargetType="{x:Type Button}"> <Setter Proper ...

  10. 通过ipv6访问 g o o g l e

    Google.Youtube.Facebook等均支持IPv6访问,IPv4网络的用户大部分都无法访问,比如Gmail,Google Docs等等各种相关服务.而该类网站大部分均已接入IPv6网络,因 ...

随机推荐

  1. centos6.4下安装php的imagick和imagemagick扩展教程

    imagick在centos6.4的安装方法: .安装ImageMagick 代码如下: wget http://soft.vpser.net/web/imagemagick/ImageMagick- ...

  2. 类模板Queue的实现

    #include <iostream> #include <vector> using namespace std; template <class Type> c ...

  3. 用JAXP的SAX方式解析XML文件

    简单用JAXP的SAX方式(事件驱动)解析XML文件: 文件(1.XML) <?xml version="1.0" encoding="UTF-8" st ...

  4. Android开发中Handler的经典总结

    当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI控件,进行事件分发. AD: 一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线 ...

  5. loadrunner获取本机的机器名称

  6. HTTP基础09--web(1)

    Web 应用 通过 Web 提供功能的 Web 应用 原本应用 HTTP 协议的 Web 的机制就是对客户端发来的请求,返回事前准备好的内容.可随着 Web 越来越普及,仅靠这样的做法已不足以应对所有 ...

  7. mybatis做like模糊查询

    http://www.cnblogs.com/cyttina/p/3894428.html

  8. iOS __strong __weak @Strongify @Weakify

    @Strongify,@Weakify主要是在block中使用. 因为block一般都在对象内部声明.. 如果在block内部使用了当前对象的属性,就会造成循环引用(block拥有当前对象的地址,而当 ...

  9. 每天一个linux命令---kill

    linux中终止进程的命令--kill 一般用的是: 搜索pid: ps -ef|grep calendar 杀死pid:kill -9 pid 格式是:kill[参数][进程号]

  10. Linux chkconfig 命令详解

    一.简介 chkconfig 命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 二.安装&启动 us ...