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.

InputThe 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 
OutputFor 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

题目大意:

有两个人(用Y和M表示)要到同一个KFC(用@表示),且存在多个KFC,找一个KFC使得两人到改KFC的总时间最短,输出该最短时间。

思路:

用两次bfs分别求出Y和M到各个KFC的最短时间,可以开一个数组储存每个KFC的时间,最后相加两者的时间并取最小值。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define MAX 0x3f3f3f3f
using namespace std;
char map[][]; //用来储存原始地图
struct node
{
int x;
int y;
int step;
};
int n, m;
int T[][]; //记录时间
int nextd[][] = { ,,,-,,,-, }; //4个运动方向
void bfs(int x, int y)
{
bool vis[][]; //记录是否走过该点,true为走过,false为未经过。
memset(vis, false, sizeof(vis));
queue<node>q; //常规的bfs
node t, s;
vis[x][y] = true;
s.x = x;
s.y = y;
s.step = ;
q.push(s);
while (!q.empty())
{
s = q.front();
q.pop();
if (map[s.x][s.y] == '@')
{
if (T[s.x][s.y] == MAX) //判断Y是否已经到改@
T[s.x][s.y] = s.step;
else
T[s.x][s.y] += s.step;
}
for (int i = ; i < ; i++)
{
t.x = s.x + nextd[i][];
t.y = s.y + nextd[i][];
t.step = s.step + ;
if (t.x < || t.x >= n || t.y < || t.y >= m) continue; //出界
if (vis[t.x][t.y]) continue; //到过该点
if (map[t.x][t.y] == '#') continue; //不可经过的点
vis[t.x][t.y] = true;
q.push(t);
}
}
} int main()
{
node M, Y;
int i, j;
while (~scanf("%d %d", &n, &m))
{
memset(T, 0x3f, sizeof(T));
for (i = ; i < n; i++) scanf("%s", map[i]);
for (i = ; i < n; i++)
{
for (j = ; j < m; j++)
{
if (map[i][j] == 'M')
{
M.x = i;
M.y = j;
}
if (map[i][j] == 'Y')
{
Y.x = i;
Y.y = j;
}
}
}
/*for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",T[i][j]);
}printf("\n");
} */
bfs(Y.x, Y.y);
/*for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",T[i][j]);
}printf("\n");
} */
bfs(M.x, M.y);
/*for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",T[i][j]);
}printf("\n");
} */
int min = MAX;
for (i = ; i < n; i++) //找到最小的总时间
{
for (j = ; j < m; j++)
{
if (min >T[i][j])
min = T[i][j];
}
}
printf("%d\n", min*);
}
return ;
}

不给看

  

最新文章

  1. 12.JAVA之GUI编程打开与保存文件
  2. C语言_第三章
  3. Git代码管理常用命令
  4. oracle之rownum(伪列)
  5. php中的常用数组函数(五)(数组中获取键名集合)
  6. Boundary Following Algorithm
  7. Mysql 列转行统计查询 、行转列统计查询
  8. bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能
  9. [转].NET下读取PDF文本
  10. matlab练习程序(多圆交点)
  11. js:数据结构笔记1---数组
  12. 临时改GCC编译器,重启后失效
  13. NGINX的奇淫技巧 —— 6. IF实现数学比较功能 (1)
  14. Setting Up the ADT Bundle
  15. GridLookUpEdit 简单应用
  16. winPcap_2_编译环境*注意*
  17. css3波浪形loading动画
  18. 填坑实录 Android Studio 利用 ADB WIFI 插件实现真机无线调试
  19. 使用Servlet实现上传文件功能
  20. win10 3dmax 激活后反复激活和激活码无效问题

热门文章

  1. iview+vue 表格任一项实现鼠标划上显示内容
  2. Javascript对checkbox勾选判断,错误提示和按钮变色操作
  3. mvc 当中 [ValidateAntiForgeryToken] 的作用 转载https://www.cnblogs.com/hechunming/p/4647646.html
  4. USACO 6.5 章节 世界上本没有龙 屠龙的人多了也便有了
  5. 3.jmeter jsr232 脚本获取当前测试的正在活动的线程数
  6. undefined,null,var 0 = {},var s = &#39;&#39;,的区别
  7. iphoneX的适配问题
  8. MVC的布局页,视图布局页和分布页的使用
  9. ps学习记录
  10. 自定义 MessageBox 组件