HDU-2612-Find a way
Posted 1625--h
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-2612-Find a way相关的知识,希望对你有一定的参考价值。
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
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
分析
1. 需要单独记录下两人的坐标,我们可以在输入的时候就可以记录下来。
2. 要求他两到同一个KFC的最短时间,所以再广搜的时候,要在队列元素中有一个记录时间的元素。
细节
要保证KFC两个人都可以到,才可以进行比较
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 struct node 5 { 6 int x, y, t; 7 node(int a, int b, int c) 8 { 9 x = a, y = b, t = c; 10 } 11 }; 12 13 char mp[203][203]; 14 int arr[203][203], may[203][203]; 15 bool mk[203][203]; 16 int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 17 18 void bfs(int bgx, int bgy) 19 { 20 memset(mk, false, sizeof(mk)); 21 queue<node> que; 22 que.push(node(bgx, bgy, 0)); 23 mk[bgx][bgy] = true; 24 while( !que.empty()) 25 { 26 node f = que.front(); 27 que.pop(); 28 for(int i=0; i<4; ++ i) 29 { 30 int x = f.x + dir[i][0], y = f.y + dir[i][1]; 31 int t = f.t + 1; 32 if(mp[x][y] != ‘#‘ && mk[x][y] == false) 33 { 34 mk[x][y] = true; 35 if(mp[x][y] == ‘@‘) 36 arr[x][y] += t, may[x][y] += 1; 37 que.push(node(x, y, t)); 38 } 39 } 40 } 41 } 42 int main() 43 { 44 int n, m; 45 while(scanf("%d%d", &n, &m) != EOF) 46 { 47 int y_bgx, y_bgy, m_bgx, m_bgy; 48 memset(mp, ‘#‘, sizeof(mp)); 49 for(int i=1; i<=n; ++ i) 50 { 51 for(int j=1; j<=m; ++ j) 52 { 53 scanf(" %c", &mp[i][j]); 54 if(mp[i][j] == ‘Y‘) 55 y_bgx = i, y_bgy = j; 56 if(mp[i][j] == ‘M‘) 57 m_bgx = i, m_bgy = j; 58 } 59 } 60 memset(arr, 0, sizeof(arr)); 61 memset(may, 0, sizeof(may)); 62 63 bfs(y_bgx, y_bgy); 64 bfs(m_bgx, m_bgy); 65 int ans = 1000000; 66 for(int i=1; i<=n; ++ i) 67 { 68 for(int j=1; j<=m; ++ j) 69 { 70 if(mp[i][j] == ‘@‘ && may[i][j] == 2) 71 { 72 if(arr[i][j] < ans) 73 ans = arr[i][j]; 74 } 75 } 76 } 77 printf("%d ", ans * 11); 78 } 79 return 0; 80 }
以上是关于HDU-2612-Find a way的主要内容,如果未能解决你的问题,请参考以下文章