Find a way

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

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
 
题意:两个人去同一个kfc,问两个人一共走的最短路的合,kfc有多个
题解:每个人都要一次bfs,用一个kfc去找人会超时,先把所有的kfc都找到再处理
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=;
typedef pair<int,int >P;
int n,m;
int xa,xb,ya,yb;
char a[][];
int vis[][];
int d[][];
int d1[][];
struct fwe
{
int x,y;
}list[];
int dx[]={,,-,};
int dy[]={,,,-};
void bfs(int x,int y)
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
d[i][j]=INF;
queue<P>que; que.push(P(x,y));
d[x][y]=;
while(que.size())
{
P p=que.front(); que.pop();
for(int i=;i<;i++)
{
int nx=p.first+dx[i];
int ny=p.second+dy[i];
if(<=nx && nx<n && <=ny && ny<m && a[nx][ny]!='#' && d[nx][ny]==INF)
{
que.push(P(nx,ny));
d[nx][ny]=d[p.first][p.second]+;
}
} }
} void bfss(int x,int y)
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
d1[i][j]=INF;
queue<P>que;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
d1[i][j]=INF;
que.push(P(x,y));
d1[x][y]=;
while(que.size())
{
P p=que.front(); que.pop();
for(int i=;i<;i++)
{
int nx=p.first+dx[i];
int ny=p.second+dy[i];
if(<=nx && nx<n && <=ny && ny<m && a[nx][ny]!='#' && d1[nx][ny]==INF)
{
que.push(P(nx,ny));
d1[nx][ny]=d1[p.first][p.second]+;
}
} }
} int main()
{
int min;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>a[i][j];
if(a[i][j]=='Y')
{
xa=i;
ya=j;
}
if(a[i][j]=='M')
{
xb=i;
yb=j;
}
}
bfs(xa,ya);
bfss(xb,yb);
int minn=INF;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(a[i][j]=='@')
{
minn=std::min(minn,d[i][j]+d1[i][j]);
} }
cout<<minn*<<endl; }
return ;
}

最新文章

  1. C语言的内存分配
  2. centos 7 下安装numpy、scipy等python包
  3. 【JavaEE企业应用实战学习记录】authorityFilter
  4. 如何使用java自定义注解?demo
  5. Natural Language Toolkit
  6. jquery animate() 防止多次执行
  7. hdu1711Number Sequence
  8. android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
  9. Javabyte[]数组和十六进制String之间的转换Util------包含案例和代码
  10. ASP.NET MVC应用程序处理并发
  11. 图解Javascript之字符串
  12. Linux Shell——函数的使用
  13. JAVA设计模式---单例模式的几种实现方式比较
  14. [AHOI 2016初中组]迷宫
  15. [HAOI2007]分割矩阵
  16. 配置rsync+inotify实时同步
  17. Canvas 旋转的图片
  18. 2015-09-21 css学习1
  19. NYOJ 12:喷水装置(二)(贪心,区间覆盖问题)
  20. (转)ASP.NET MVC 3和Razor中的@helper 语法

热门文章

  1. aix OPATH ISSUE
  2. HDU 1087 E - Super Jumping! Jumping! Jumping! DP
  3. C#基础之运行环境
  4. [转载]&mdash;Health Check Reports Problem: Dependency$ p_timestamp mismatch for VALID objects (文档 ID 781959.1)
  5. WinForm 窗体API移动 API阴影
  6. MongoDB远程定时备份与还原
  7. UVA12897 - Decoding Baby Boos
  8. opencv中mat的type
  9. superset docker 部署
  10. HTML5&lt;article&gt;元素