D - Find a way

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

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
 
 
//首先,我的思路是找到一个KFC就从两个点出发,去寻找这个KFC,不断刷新最小的距离,但是这样会超时。
去网上看了一下,换了一种做法,先从两个点出发,去遍历一次地图,将到达地图任意可达的位置的最小步数记录下来,然后在用循环遍历一次地图,找到最小之和。
 
 

#include <iostream>
#include <queue>
#include <string.h>
using namespace std; struct point
{
int x,y;
int step;
};
point py,pm;
int m,n;
char map[][];
int test_y[][];
int test_m[][];
int min_all; void Init()
{
for (int i=;i<=m;i++)
{
for (int j=;j<=n;j++)
{
cin>>map[i][j];
if (map[i][j]=='Y')
{
py.x=i;
py.y=j;
py.step=;
}
if (map[i][j]=='M')
{
pm.x=i;
pm.y=j;
pm.step=;
}
}
}
} int check_y(point t)
{
if (t.x<=m&&t.x>=&&t.y>=&&t.y<=n&&test_y[t.x][t.y]==&&map[t.x][t.y]!='#')
return ;
return ;
} int check_m(point t)
{
if (t.x<=m&&t.x>=&&t.y>=&&t.y<=n&&test_m[t.x][t.y]==&&map[t.x][t.y]!='#')
return ;
return ;
} void bfs()
{
queue<point> Q;
point now,next; while (!Q.empty()) Q.pop();
memset(test_y,,sizeof(int)*(m+)*); now.x=py.x;
now.y=py.y;
now.step=;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop(); next.x=now.x+;
next.y=now.y;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y-;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;} next.x=now.x-;
next.y=now.y;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y+;
next.step=now.step+;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;}
} while (!Q.empty()) Q.pop();
memset(test_m,,sizeof(int)*(m+)*); now.x=pm.x;
now.y=pm.y;
now.step=;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop(); next.x=now.x+;
next.y=now.y;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y-;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;} next.x=now.x-;
next.y=now.y;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;} next.x=now.x;
next.y=now.y+;
next.step=now.step+;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;}
}
} int main()
{
while (cin>>m>>n&&m&&n)
{
Init();
bfs(); min_all=;
for (int i=;i<=m;i++)
{
for (int j=;j<=n;j++)
{
if (map[i][j]=='@' && test_y[i][j]+test_m[i][j] < min_all&& test_y[i][j]!= && test_m[i][j]!= )//
{
min_all=test_y[i][j]+test_m[i][j];
}
}
}
cout<<min_all*<<endl;
}
return ;
}

 

随机推荐

  1. java 入门-helloWorld

    Java 教程 Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统. ...

  2. 【BIEE】19_不齐整维和越级维

    不齐整维:没有子节点的维度 越级维:层级维度出现断裂,则称为越级维 下图我们就可以清晰的看出: 首先,我们将表导入到资料库做好与事实表的关联后并建立相应维 以下是按照一般维度创建维后的结果 创建完成之 ...

  3. HDU 4287 Intelligent IME(map运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...

  4. Mysql中处理1970年前的日期(unixtime为负数的情况)负数时间戳格式化

    客户扔过来一个bug,说是一个系统中对42岁以上的人的统计不正确,而41岁以下的人没有问题.眼睛瞟了一下托盘区里的日期,2012年3月26日,嗯,今年42岁的话,那么应该就是出生在1970年左右,马上 ...

  5. linux中的dd复制命令

    dd命令用于复制文件并对原文件的内容进行转换和格式化处理.dd命令功能很强大的,对于一些比较底层的问题,使用dd命令往往可以得到出人意料的效果.用的比较多的还是用dd来备份裸设备.但是不推荐,如果需要 ...

  6. unity, 烘焙lightmap

    1,建一个名为_scene的场景,放一个球体. 2,将球体的Static属性勾选. 3,将默认光源Directional light的Baking属性改为Baked. 4,打开Window->L ...

  7. codeforces Round #Pi (div.2) 567ABCD

    567A Lineland Mail题意:一些城市在一个x轴上,他们之间非常喜欢写信交流.送信的费用就是两个城市之间的距离,问每个城市写一封信给其它城市所花费的最小费用和最大的费用. 没什么好说的.直 ...

  8. C语言基础(21)-C语言编译过程及GCC参数简介

    任何C语言的编译过程可分为以下三部分: 一.预编译 在C语言中,以#开头的语句又叫预编译指令.预编译主要做以下两件事情: 1.将#include包含的头文件做简单的文本替换: 2.将代码中的注释删除. ...

  9. PILE读书笔记_基础知识

    程序的构成 Linux下二进制可执行程序的格式一般为ELF格式. 我们可以用readelf命令来读取二进制的信息. ELF文件的主要内容就是由各个section及symbol表组成的. 下面来分别介绍 ...

  10. mysql之replicate_do_table/replicate_ingore_table/replicate_wide_do_table/replicate_wide_ingore_table

    参考:http://yhqlzz.blog.51cto.com/2557314/1159084/ mysql官网:http://dev.mysql.com/doc/refman/5.1/en/repl ...