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. 一起学微软Power BI系列-使用技巧(2)连接Excel数据源错误解决方法
  2. centos7 系统初始化脚本
  3. 孙鑫MFC学习笔记12:文件读写
  4. 实战手记:让百万级数据瞬间导入SQL Server
  5. 【转帖】Moodle平台的5个新玩法
  6. Etl之HiveSql调优(left join where的位置)
  7. STL容器删除元素的陷阱
  8. js 中数组或者对象的深拷贝和浅拷贝
  9. IOS判断网络环境
  10. 【Irrlicht鬼火引擎】 安装配置Irrlicht鬼火引擎
  11. QT运行时加载UI文件
  12. 【动态规划】【二分】【最长上升子序列】Vijos P1028 魔族密码
  13. linux下解压压缩rar文件
  14. document.cookie
  15. QML基础(六篇文章)
  16. VirtualBox安装ubuntu14.04和文件共享
  17. Java 9 揭秘(9. 打破模块封装)
  18. 【学习笔记】C# 抽象类
  19. htmlcss渐变及兼容性
  20. css中文字体解决方案

热门文章

  1. Srping AOP xml方式
  2. redislive
  3. mac系统中搭建apache+mysql+php的开发环境,安装mysql后,登录报错:mac ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: NO)
  4. ASP.NET MVC:创建 ModelBinder 自动 Trim 所有字符串
  5. Android模拟器Genymotion安装apk
  6. 深入分析JavaWeb Item22 -- 国际化(i18n)
  7. gsub函数
  8. DataUml Design 介绍8-DataUML 1.2版本正式发布(支持SQLite数据库、NetUML开发框架)
  9. Lucene.Net 介绍
  10. python 开发技巧(3)-- 连接mysql 出现错误 ModuleNotFoundError: No module named &#39;MySQLdb&#39;