Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16184    Accepted Submission(s): 5194

Problem 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
 
Author
yifenfei
 
Source
 
Recommend
yifenfei

题意:Y和M在两个不同起点,他们要到KFC集合,路上有多家KFC,问到哪家KFC能使他们的步数和最少?

思路:两边bfs,分别存取Y和M到各家KFC的步数,相加求和,枚举各KFC输出最小值。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][],c[][];
int t[][]={{,},{,},{-,},{,-}}; struct Node{
int x,y,s;
}node; int main()
{
int n,m,yx,yy,mx,my,i,j;
while(~scanf("%d%d",&n,&m)){
memset(c,,sizeof(c));
queue<Node> q;
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
yx=i;
yy=j;
}
if(a[i][j]=='M'){
mx=i;
my=j;
}
}
}
memset(b,,sizeof(b));
b[yx][yy]=;
node.x=yx;
node.y=yy;
node.s=;
q.push(node);
while(q.size()){
for(i=;i<;i++){
int tx=q.front().x+t[i][];
int ty=q.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='#'||b[tx][ty]==) continue;
b[tx][ty]=;
if(a[tx][ty]=='@'){
c[tx][ty]=q.front().s+;
}
node.x=tx;
node.y=ty;
node.s=q.front().s+;
q.push(node);
}
q.pop();
}
memset(b,,sizeof(b));
b[mx][my]=;
node.x=mx;
node.y=my;
node.s=;
q.push(node);
while(q.size()){
for(i=;i<;i++){
int tx=q.front().x+t[i][];
int ty=q.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='#'||b[tx][ty]==) continue;
b[tx][ty]=;
if(a[tx][ty]=='@'){
c[tx][ty]+=q.front().s+;
}
node.x=tx;
node.y=ty;
node.s=q.front().s+;
q.push(node);
}
q.pop();
}
int min=;
for(i=;i<n;i++){
for(j=;j<m;j++){
if(c[i][j]<min&&c[i][j]!=) min=c[i][j];
}
}
printf("%d\n",min);
}
return ;
}

最新文章

  1. 部署JProfiler监控tomcat
  2. postgresql 设置只读用户
  3. Adb refused a command 解决方法
  4. Python之路-Day2
  5. TDDL DataSource
  6. ecshop JSON,ajax.call 异步传输
  7. python查找并删除相同文件-UNIQ File-wxPython版本
  8. BZOJ1004 [HNOI2008]Cards(Polya计数)
  9. Linux free命令详解(转)
  10. PSP记录表
  11. git shell 中文
  12. 疯狂java讲义——多态
  13. 1021.Deepest Root (并查集+DFS树的深度)
  14. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-005-Pizza例子的订单流程()
  15. unlinking
  16. ural2014 Zhenya moves from parents
  17. php 与redis 结合 使用predis
  18. java.lang.IllegalArgumentException: object is not an instance of declaring class
  19. idea for Mac 代码提示设置
  20. UVA 12009 - Avaricious Maryanna(数论)

热门文章

  1. vue-router篇
  2. Django-model_form
  3. Spring Boot 测试时的日志级别
  4. Jquery 插件 实例
  5. caffe2--ubuntu16.04--14.04--install
  6. 无线(仅WIFI)攻击思路总结
  7. CIDR(无类域间路由)(转载)
  8. 九度OJ 1126:打印极值点下标 (基础题)
  9. bc - An arbitrary precision calculator language
  10. 头文件---#include&lt;***.h&gt;和#include&quot;***.h&quot;的区别