Find a way

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 48   Accepted Submission(s) : 15

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem 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


———————————————————————————————————————————————————

题目说两个人,他们想在某家KCF见面,要求他们到这家KCF的时间花费最少。
我们从每个kfc出发搜到每个人,选出到两个人距离和最小的就是答案


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std; char mp[205][205];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int vir[205][205];
int m,n,ans1,ans2,mn; struct node
{
int x,y,cnt;
}; bool cheak(int i,int j)
{
if(i<1||i>m||j<1||j>n||mp[i][j]=='#')
return 0;
else
return 1;
} void bfs(int si,int sj,int yi,int yj,int mi,int mj)
{
queue<node>q;
node f,d;
memset(vir,0,sizeof(vir));
f.x=si;
f.y=sj;
f.cnt=0;
vir[f.x][f.y]=1;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if(ans1==-1&&ans2==-1&&2*f.cnt>=mn)
return;
if(ans1==-1&&ans2!=-1&&f.cnt+ans2>=mn)
return;
if(ans1!=-1&&ans2==-1&&f.cnt+ans1>=mn)
return; if(f.x==yi&&f.y==yj&&ans1==-1)
{
ans1=f.cnt;
}
if(f.x==mi&&f.y==mj&&ans2==-1)
{
ans2=f.cnt;
}
if(ans1!=-1&&ans2!=-1)
return;
for(int i=0;i<4;i++)
{
d.x=f.x+dir[i][0];
d.y=f.y+dir[i][1];
if(cheak(d.x,d.y)&&!vir[d.x][d.y])
{
d.cnt=f.cnt+1;
vir[d.x][d.y]=1;
q.push(d);
}
}
}
return ;
} int main()
{
int yi,yj,mi,mj,ans;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='Y')
{
yi=i;
yj=j;
}
if(mp[i][j]=='M')
{
mi=i;
mj=j;
}
}
mn=100000;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
if(mp[i][j]=='@')
{
ans1=-1;
ans2=-1;
bfs(i,j,yi,yj,mi,mj);
if(ans1!=-1&&ans2!=-1)
{
ans=ans1+ans2;
if(ans<mn)
mn=ans;
}
}
}
printf("%d\n",mn*11); }
return 0;
}

Hdu2612 Find a way 2017-01-18 14:52 59人阅读 评论(0) 收藏的更多相关文章

  1. 安装spark1.3.1单机环境 分类: B8_SPARK 2015-04-27 14:52 1873人阅读 评论(0) 收藏

    本文介绍安装spark单机环境的方法,可用于测试及开发.主要分成以下4部分: (1)环境准备 (2)安装scala (3)安装spark (4)验证安装情况 1.环境准备 (1)配套软件版本要求:Sp ...

  2. Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏

    哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  3. Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏

    以下程序在hadoop1.2.1上测试成功. 本例先将源代码呈现,然后详细说明执行步骤,最后对源代码及执行过程进行分析. 一.源代码 package org.jediael.hadoopdemo.wo ...

  4. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  5. iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  6. iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏

    //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...

  7. 第十二届浙江省大学生程序设计大赛-May Day Holiday 分类: 比赛 2015-06-26 14:33 10人阅读 评论(0) 收藏

    May Day Holiday Time Limit: 2 Seconds Memory Limit: 65536 KB As a university advocating self-learnin ...

  8. 第十二届浙江省大学生程序设计大赛-Lunch Time 分类: 比赛 2015-06-26 14:30 5人阅读 评论(0) 收藏

    Lunch Time Time Limit: 2 Seconds Memory Limit: 65536 KB The 999th Zhejiang Provincial Collegiate Pro ...

  9. Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. Java Magic. Part 4: sun.misc.Unsafe

    Java Magic. Part 4: sun.misc.Unsafe @(Base)[JDK, Unsafe, magic, 黑魔法] 转载请写明:原文地址 系列文章: -Java Magic. P ...

  2. Dubbo 分布式服务框架

    要想了解Dubbo是什么,我们不防先了解它有什么用. 使用场景:比如我想开发一个网上商城项目,这个网上商城呢,比较复杂,分为pc端web管理后台,微信端销售公众号,那么我们分成四个项目,pc端网站,微 ...

  3. inner outer

    left join 是left outer join的简写,left join默认是outer属性的. Inner JoinInner Join 逻辑运算符返回满足第一个(顶端)输入与第二个(底端)输 ...

  4. 在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味

    在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味

  5. Eclipse中的SVN操作

    --------------------siwuxie095                                     Eclipse 中的 SVN 操作         (一)发布项目 ...

  6. jsoncpp在Windows和Linux下的安装

    Windows下: 参考这个网站,没什么问题,注意MTd这些选对就行了. http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.h ...

  7. Swift 项目中可能用到的第三方框架

    这里记录下swift开发中可能用的框架 , 最近浏览了不少,积累在这里,以后用的时候方便查阅.顺便推荐给大家! 这里的框架都是纯swift的 , 感谢开源 ,感谢大神们 . 下拉刷新 BreakOut ...

  8. [leetcode]250. Count Univalue Subtrees统计节点值相同的子树

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  9. 帧动画布局文件 animation-list

    <?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android=&qu ...

  10. PAT 1077 互评成绩计算(20)(代码+思路)

    1077 互评成绩计算(20 分) 在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一 ...