题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2612
Find a way
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.
Input
The input contains multiple test cases.
Each test case include, first two integers $n, m. (2 leq n,m leq 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
两次bfs,注意若有一方无法到达某个kfc,这个点就不能取。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<map> 9 using std::min; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 using std::find; 14 using std::sort; 15 using std::map; 16 using std::pair; 17 using std::queue; 18 using std::vector; 19 using std::multimap; 20 #define pb(e) push_back(e) 21 #define sz(c) (int)(c).size() 22 #define mp(a, b) make_pair(a, b) 23 #define all(c) (c).begin(), (c).end() 24 #define iter(c) decltype((c).begin()) 25 #define cls(arr,val) memset(arr,val,sizeof(arr)) 26 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 27 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 28 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 29 const int N = 210; 30 const int INF = ~0u >> 1; 31 typedef unsigned long long ull; 32 char G[N][N]; 33 int H, W, Sx, Sy, Dx, Dy, dist[2][N][N]; 34 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 }; 35 struct Node { 36 int x, y; 37 Node(int i = 0, int j = 0) :x(i), y(j) {} 38 }Y, M; 39 void bfs(int x, int y, bool d) { 40 queue<Node> q; 41 q.push(Node(x, y)); 42 dist[d][x][y] = 0; 43 while (!q.empty()) { 44 Node t = q.front(); q.pop(); 45 rep(i, 4) { 46 int nx = t.x + dx[i], ny = t.y + dy[i]; 47 if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue; 48 if (G[nx][ny] == '#' || dist[d][nx][ny]) continue; 49 dist[d][nx][ny] = dist[d][t.x][t.y] + 1; 50 q.push(Node(nx, ny)); 51 } 52 } 53 } 54 int main() { 55 #ifdef LOCAL 56 freopen("in.txt", "r", stdin); 57 freopen("out.txt", "w+", stdout); 58 #endif 59 while (~scanf("%d %d", &H, &W)) { 60 cls(dist, 0); 61 vector<Node> kfc; 62 rep(i, H) { 63 scanf("%s", G[i]); 64 rep(j, W) { 65 if (G[i][j] == 'Y') Sx = i, Sy = j; 66 if (G[i][j] == 'M') Dx = i, Dy = j; 67 if (G[i][j] == '@') kfc.pb(Node(i, j)); 68 } 69 } 70 bfs(Sx, Sy, 0), bfs(Dx, Dy, 1); 71 int res = INF; 72 rep(i, sz(kfc)) { 73 Node &k = kfc[i]; 74 int A = dist[0][k.x][k.y], B = dist[1][k.x][k.y]; 75 if (!A || !B) continue; 76 res = min(res, A + B); 77 } 78 printf("%d ", res * 11); 79 } 80 return 0; 81 }