http://acm.hdu.edu.cn/showproblem.php?pid=2612
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6065 Accepted Submission(s): 2029
#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; }