Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10324 Accepted Submission(s): 3365
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
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; struct node{ int x, y, step; }a, b, temp; bool vis[300][300]; char maze[300][300]; int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; int ans[300][300], ans1[300][300]; int m, n, temp1, temp2, sx1, sx2, sy1, sy2; int bfs(int x, int y, bool flag) { queue<node> que; memset(vis, false, sizeof(vis)); while (!que.empty()) que.pop(); temp.x = x; temp.step = 0; temp.y = y; vis[x][y] = true; que.push(temp); while (que.size()) { a = que.front(); que.pop(); if (maze[a.x][a.y] == '@') { //找到后就记录结果。 if (flag) ans1[a.x][a.y] = a.step; else ans[a.x][a.y] = a.step; } for (int i = 0; i < 4; i++) { b.x = a.x + dx[i]; b.y = a.y + dy[i]; b.step = a.step + 1; if (b.x < 0 || b.y < 0 || b.x >= n || b.y >= m || maze[b.x][b.y] == '#') continue; if (vis[b.x][b.y]) continue; vis[b.x][b.y] = true; que.push(b); } } } int main() { while (scanf("%d%d", &n, &m) != EOF) { memset(ans, 0, sizeof(ans)); memset(ans1, 0, sizeof(ans1)); for (int i = 0; i < n; i++) { scanf("%s", maze[i]); for (int j = 0; j < m; j++) { if (maze[i][j] == 'Y') { sx1 = i, sy1 = j; } else if (maze[i][j] == 'M') { sx2 = i, sy2 = j; } } } bool flag = false; bfs(sx1, sy1, flag); bfs(sx2, sy2, flag = true); int cnt = 1e12; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { //必须这个点被同时访问到 if(ans1[i][j] != 0 && ans[i][j] != 0) cnt = min(cnt, ans[i][j] + ans1[i][j]); } } printf("%d ", cnt*11); } return 0; }