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. ubuntu挂载3T新硬盘并更换home分区

    ubuntu添加一块新的硬盘,并更换home分区 1.将硬盘接入机箱,开机,查看是否现有的硬盘信息 从这里可以看出,共有两块硬盘,分别是sda和sdb 2.执行fdisk -l,确保我们要添加的就是s ...

  2. 【Java】Java_05 标识符与字符集

    1.标识符 用作给变量.类和方法命名.注意: 表示类名的标识符用大写字母开始.如:Man, GoodMan 表示方法和变量的标识符用小写字母开始,后面的描述性词以大写开始.green(),greenC ...

  3. rtp协议详解/rtcp协议详解

    转自:http://www.cnblogs.com/li0803/archive/2010/11/20/1882792.html 1.简介 目前,在IP网络中实现实时语音.视频通信和应用已经成为网络应 ...

  4. SQL中的ISNULL函数介绍

    SQL中有多种多样的函数,下面将为您介绍SQL中的ISNULL函数,包括其语法.注释.返回类型等,供您参考,希望对您学习SQL能够有所帮助. ISNULL 使用指定的替换值替换 NULL. 语法ISN ...

  5. systemd 对/home等目录的限制

    systemd 默认配置了对/root /home等目录的限制 比如ProtectHome=true 意味着启动时应用对这些目录不可写,这是个大坑,谨记

  6. atitit.atiOrmStoreService 框架的原理与设计 part1  概述与新特性

    atitit.atiOrmStoreService 框架的原理与设计 part1  概述与新特性 1. 新特性如下 支持生成sql在无数据库连接的情况下 2. Orm设计 主要的俩个以来service ...

  7. Atitit. Derby的使用总结attilax

    Atitit. Derby的使用总结attilax 1. Derby数据库的存储结构1 2. Derby gui工具1 3. 代码读写derby2 4. 与sqlite db4o的对比4 5. 参考5 ...

  8. osd char

    osdchar.c #include<stdio.h> #include "TimeNewRoman.h" #define TNR 0x00 //Time New Ro ...

  9. Vue 组件 非父子组件通信

    有时候两个组件也需要通信(非父子关系),在简单的场景下,可以使用一个空的vue实例作为中央事件总线: var bus = new Vue(); //触发组件a中的事件 bus.$emit('id-se ...

  10. FreeMarker中在list中加入if判断

    例如list中遍历releaseitem,在ri中获取audit的值,如果audit的值为0则表示正在审核中,如果为1则表示审核通过,如果为2则表示未审核. <#list releaseitem ...