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
 
 
问题分析:依旧是bfs,不过要考虑kfc被包围,以及重置地图,和两人所占位都是可以互相走过的。
 #include "iostream"
#include "queue"
using namespace std;
struct ma
{
char z;
int sum;
};
struct person
{
int i;
int j;
int time;
};
int v[][] ={,,-,,,-,,};
ma maze[][];
ma maze2[][];
person fir,sec;
void scopy(int n,int m)
{
for (int i=;i<=n+;i++)
for (int j=;j<=m+;j++)
{
if (maze2[i][j].z == '@')
maze[i][j].z = '@';
else
maze[i][j]=maze2[i][j];
}
}
void mbegin(int n,int m)
{
int i,j;
for (i=;i<=n+;i++)
for (j=;j<=m+;j++)
if (i*j == || i == n+ || j ==m+)
maze2[i][j].z ='#';
else
{
cin>>maze2[i][j].z;
maze2[i][j].sum=;
maze[i][j].sum=;
if (maze2[i][j].z == 'Y')
{
fir.i = i;
fir.j = j;
}
if (maze2[i][j].z == 'M')
{
sec.i =i;
sec.j =j;
}
}
}
void bfs(person &then)
{
queue<person> p;
person next;
then.time=;
maze[then.i][then.j].z = '#';
p.push(then);
while (!p.empty())
{
next = p.front();
p.pop();
for (int k=;k<;k++)
{
then.i = next.i+v[k][];
then.j = next.j+v[k][];
if (maze[then.i][then.j].z != '#')
{
then.time = next.time+;
if (maze[then.i][then.j].z == '@')
maze[then.i][then.j].sum += then.time;
maze[then.i][then.j].z ='#';
p.push(then);
}
}
}
}
int mintime(int n,int m)
{
int k=,time;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (maze[i][j].z == '@')
{
if (maze[i][j].sum == )
continue;
if (k++ == )
time=maze[i][j].sum;
if (time > maze[i][j].sum)
time=maze[i][j].sum;
}
return time;
}
int main()
{
int n,m;
while (cin>>n>>m)
{
mbegin(n,m);
scopy(n,m);
bfs(fir);
scopy(n,m);
bfs(sec);
scopy(n,m);
cout<<mintime(n,m)<<endl;
}
return ;
}

暑假集训(1)第四弹 -----Find a way(Hdu2612)的更多相关文章

  1. 暑假集训(4)第四弹 -----排列,计数(hdu1465)

    题意概括:嗯,纵使你数次帮助小A脱离困境,但上一次,小A终于还是失败了.那数年的奔波与心血,抵不过轻轻一指,便彻底 湮灭,多年的友谊终归走向末路.这一切重击把小A彻底击溃! 不为什么,你到底还是要继续 ...

  2. 暑假集训(3)第四弹 -----Frogger(Poj2253)

    题意梗概:青蛙王子最近喜欢上了另一只经常坐在荷叶上的青蛙公主.不过这件事不小心走漏了风声,被某fff团团员知 道了,在青蛙王子准备倾述心意的那一天,fff团团员向湖泊中注入大量的充满诅咒力量的溶液.这 ...

  3. 暑假集训(2)第四弹 ----- 敌兵布阵(hdu1166)

    D - 敌兵布阵 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit ...

  4. 暑假集训(2)第七弹 -----今年暑假不AC(hdu2037)

    J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64 ...

  5. 暑假集训(4)第五弹——— 数论(hdu1222)

    题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A ...

  6. 暑假集训(4)第八弹——— 组合(hdu1524)

    题意概括:你已经赢得两局,最后一局是N个棋子往后移动,最后一个无法移动的玩家失败. 题目分析:有向无环图sg值游戏,尼姆游戏的抽象表达.得到每个棋子的sg值之后,把他们异或起来,考察异或值是否为0. ...

  7. 暑假集训(4)第七弹——— 组合(hdu1850)

    题意概括:你赢得了第一局.魔鬼给出的第二局是,如果有N堆牌,先手的人有几种可能胜利. 问题分析:尼姆游戏,先得到n堆牌的数量异或和,再将异或和与每一个牌组的数量异或,如果结果小于原牌组数量 则可能++ ...

  8. 暑假集训(4)第六弹——— 组合(poj1067)

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

  9. 暑假集训(4)第三弹 -----递推(Hdu1799)

    问题描述:还记得正在努力脱团的小A吗? 他曾经最亲密的战友,趁他绘制贤者法阵期间,暗中设下鬼打墙将小A 围困,并准备破坏小A正在绘制的法阵.小A非常着急.想阻止他的行动.而要阻止他,必须先破解鬼打墙. ...

随机推荐

  1. HW1.7

    public class Solution { public static void main(String[] args) { System.out.println("π = " ...

  2. Kooboo中怎么新增一个关联的Details 动态页面。

    Kooboo中怎么新增一个关联的Details 动态页面. 有几个要点: 1. Sub Page的Parent Page 必须是英文书写.如果是中文会出现找不到页面 500错误 2. 要在Page M ...

  3. mysql 中文乱码的解决办法

    I would not suggest Richies answer, because you are screwing up the data inside the database. You wo ...

  4. Android camera采集视频 X264编码

    参考 http://blog.csdn.net/zblue78/article/details/6058147 感谢 ExperiencesOfCode 硬件平台:CPU Intel G630 @2. ...

  5. MySQL索引和优化查询

    索引和优化查询 恰当的索引可以加快查询速度,可以分为四种类型:主键.唯一索引.全文索引.普通索引. 主键:唯一且没有null值. create table pk_test(f1 int not nul ...

  6. vi / vim 删除以及其它命令

    删除一行:dd 删除一个单词/光标之后的单词剩余部分:dw 删除当前字符:x 光标之后的该行部分:d$ 文本删除 dd 删除一行 d$ 删除以当前字符开始的一行字符 ndd 删除以当前行开始的n行 d ...

  7. 第一章 Windows NT System Components

    Page 3. The focus(焦点) of this book is Windows NT file system and the interaction(交互) of the file sys ...

  8. excel函数

    120.623652,31.386228 120.623652 31.386228 上面数据要分成两列数据,我用了函数 =LEFT(C4,FIND(",",C4)-1),=RIGH ...

  9. sping获取bean方法 解决资源耗尽

    // ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationCon ...

  10. jQuer基础

    一.概述 jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more.如今,jQuery已经成为最流行的javascript库,在世界前10000个访问最多 ...