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.
InputThe 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
OutputFor 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使得两人到改KFC的总时间最短,输出该最短时间。
思路:
用两次bfs分别求出Y和M到各个KFC的最短时间,可以开一个数组储存每个KFC的时间,最后相加两者的时间并取最小值。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define MAX 0x3f3f3f3f 6 using namespace std; 7 char map[210][210]; //用来储存原始地图 8 struct node 9 { 10 int x; 11 int y; 12 int step; 13 }; 14 int n, m; 15 int T[210][210]; //记录时间 16 int nextd[4][2] = { 0,1,0,-1,1,0,-1,0 }; //4个运动方向 17 void bfs(int x, int y) 18 { 19 bool vis[210][210]; //记录是否走过该点,true为走过,false为未经过。 20 memset(vis, false, sizeof(vis)); 21 queue<node>q; //常规的bfs 22 node t, s; 23 vis[x][y] = true; 24 s.x = x; 25 s.y = y; 26 s.step = 0; 27 q.push(s); 28 while (!q.empty()) 29 { 30 s = q.front(); 31 q.pop(); 32 if (map[s.x][s.y] == '@') 33 { 34 if (T[s.x][s.y] == MAX) //判断Y是否已经到改@ 35 T[s.x][s.y] = s.step; 36 else 37 T[s.x][s.y] += s.step; 38 } 39 for (int i = 0; i < 4; i++) 40 { 41 t.x = s.x + nextd[i][0]; 42 t.y = s.y + nextd[i][1]; 43 t.step = s.step + 1; 44 if (t.x < 0 || t.x >= n || t.y < 0 || t.y >= m) continue; //出界 45 if (vis[t.x][t.y]) continue; //到过该点 46 if (map[t.x][t.y] == '#') continue; //不可经过的点 47 vis[t.x][t.y] = true; 48 q.push(t); 49 } 50 } 51 } 52 53 int main() 54 { 55 node M, Y; 56 int i, j; 57 while (~scanf("%d %d", &n, &m)) 58 { 59 memset(T, 0x3f, sizeof(T)); 60 for (i = 0; i < n; i++) scanf("%s", map[i]); 61 for (i = 0; i < n; i++) 62 { 63 for (j = 0; j < m; j++) 64 { 65 if (map[i][j] == 'M') 66 { 67 M.x = i; 68 M.y = j; 69 } 70 if (map[i][j] == 'Y') 71 { 72 Y.x = i; 73 Y.y = j; 74 } 75 } 76 } 77 /*for(i=0;i<n;i++) 78 { 79 for(j=0;j<m;j++) 80 { 81 printf("%d ",T[i][j]); 82 }printf(" "); 83 } */ 84 bfs(Y.x, Y.y); 85 /*for(i=0;i<n;i++) 86 { 87 for(j=0;j<m;j++) 88 { 89 printf("%d ",T[i][j]); 90 }printf(" "); 91 } */ 92 bfs(M.x, M.y); 93 /*for(i=0;i<n;i++) 94 { 95 for(j=0;j<m;j++) 96 { 97 printf("%d ",T[i][j]); 98 }printf(" "); 99 } */ 100 int min = MAX; 101 for (i = 0; i < n; i++) //找到最小的总时间 102 { 103 for (j = 0; j < m; j++) 104 { 105 if (min >T[i][j]) 106 min = T[i][j]; 107 } 108 } 109 printf("%d ", min*11); 110 } 111 return 0; 112 }