http://acm.hdu.edu.cn/showproblem.php?pid=2612
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14080 Accepted Submission(s): 4482
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.
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
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
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<math.h> #include<algorithm> #include<vector> #include<queue> #include<map> using namespace std; const int maxn=206; const int INF=0x3f3f3f3f; int dir[4][2]= {{0,1}, {0, -1}, {1, 0}, {-1, 0}}; int dist1[maxn][maxn], dist2[maxn][maxn]; int vis[maxn][maxn]; char maps[maxn][maxn]; int n, m; struct node { int x, y, step; }; void bfs(node start, int dist[][maxn]) { memset(vis, 0, sizeof(vis)); queue<node>Q; node p, q; Q.push(start); vis[start.x][start.y]=1; dist[start.x][start.y]=0; while(!Q.empty()) { p=Q.front(); Q.pop(); for(int i=0; i<4; i++) { q.x=p.x+dir[i][0]; q.y=p.y+dir[i][1]; if(!vis[q.x][q.y]&&q.x>=0&&q.x<n&&q.y>=0&&q.y<m&&maps[q.x][q.y]!='#') { vis[q.x][q.y]=1; dist[q.x][q.y]=dist[p.x][p.y]+1; Q.push(q); } } } } int main() { while(~scanf("%d %d", &n, &m)) { node s1, s2; getchar(); for(int i=0; i<n; i++,getchar()) { for(int j=0; j<m; j++) { scanf("%c", &maps[i][j]); if(maps[i][j]=='Y') { s1.x=i; s1.y=j; s1.step=0; } if(maps[i][j]=='M') { s2.x=i; s2.y=j; s2.step=0; } } } memset(dist1, 0, sizeof(dist1)); memset(dist2, 0, sizeof(dist2)); bfs(s1, dist1); bfs(s2, dist2); int Min=INF; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(maps[i][j]=='@'&&dist1[i][j]!=0&&dist2[i][j]!=0) Min=min(Min, dist1[i][j]+dist2[i][j]); } } printf("%d ", Min*11); } return 0; }