HDU 2612 Find a way
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): 6065 Accepted Submission(s): 2029
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
题目大意:Y和M要在一家KFC见面, 这个城市有很多个KFC,问他们到KFC最少用时,每步用时11m
aim[i][j][0] : Y到每个点的步数
aim[i][j][1] : M到每个点的步数
结果找二者和最小的值
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<vector> #include<queue> #define min(a, b)(a < b ? a : b) #define INF 0x3f3f3f3f #define N 210 using namespace std; struct node { int x, y, step; }; char maps[N][N]; int m, n, f, aim[N][N][2]; bool vis[N][N]; int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; void BFS(int x, int y) { queue<node>Q; node now, next; int i, a, b; now.x = x; now.y = y; now.step = 0; vis[x][y] = true; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); for(i = 0 ; i < 4 ; i++) { a = next.x = now.x + d[i][0]; b = next.y = now.y + d[i][1]; if(a >= 0 && a < m && b >= 0 && b < n && !vis[a][b] && maps[a][b] != '#') { next.step = now.step + 1; if(maps[a][b] == '@') aim[a][b][f] = next.step; vis[a][b] = true; Q.push(next); } } } } int main() { int i, j, ans; while(scanf("%d%d", &m, &n) != EOF) { ans = INF; for(i = 0 ; i < m ; i++) { for(j = 0 ; j < n ; j++) aim[i][j][0] = aim[i][j][1] = INF; } for(i = 0 ; i < m ; i++) scanf("%s", maps[i]); for(i = 0 ; i < m ; i++) { for(j = 0 ; j < n ; j++) { if(maps[i][j] == 'Y') { f = 0; memset(vis, false, sizeof(vis)); BFS(i, j); } if(maps[i][j] == 'M') { f = 1; memset(vis, false, sizeof(vis)); BFS(i,j); } } } for(i = 0 ; i < m ; i++) { for(j = 0 ; j < n ; j++) ans = min(ans, aim[i][j][0] + aim[i][j][1]); } printf("%d\n", ans * 11); } return 0; }