首页 > 代码库 > HDU - 2612 - Find a way (BFS)
HDU - 2612 - Find a way (BFS)
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6157 Accepted Submission(s): 2052
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
Author
yifenfei
Source
奋斗的年代
简单的BFS题,bfs来记录Y和M到每一个点的最小步数。这里只是要注意可能会有的KFC不能到达
AC代码:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; int n, m; int mp[205][205]; int d[205][205]; int vis[205][205]; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; struct node { int a; int b; int cnt; node (int x, int y, int z) { a = x; b = y; cnt = z; } }; void bfs(int x, int y) { //bfs遍历来存储M和Y到每一个点的距离 memset(vis, 0, sizeof(vis)); queue<node> que; que.push(node(x, y, 0)); vis[x][y] = 1; while(!que.empty()) { node t = que.front(); que.pop(); for(int i = 0; i < 4; i ++) { int xx = t.a + dx[i]; int yy = t.b + dy[i]; if(xx >= 0 && xx <= n - 1 && yy >= 0 && yy <= m - 1 && mp[xx][yy] != 2 && !vis[xx][yy]) { que.push(node(xx, yy, t.cnt + 1)); d[xx][yy] += t.cnt + 1; vis[xx][yy] = 1; } } } } int main() { while(scanf("%d %d", &n, &m) != EOF) { int yx, yy; int mx, my; char s[205]; for(int i = 0; i < n; i ++) { scanf("%s", &s); for(int j = 0; j < m; j ++) { if(s[j] == ‘.‘) mp[i][j] = 1; else if(s[j] == ‘#‘) mp[i][j] = 2; else if(s[j] == ‘@‘) { mp[i][j] = 3; } else if(s[j] == ‘Y‘) { mp[i][j] = 4; yx = i; yy = j; } else if(s[j] == ‘M‘) { mp[i][j] = 4; mx = i; my = j; } } } // for(int i = 0; i < n; i ++, cout << endl) { // for(int j = 0; j < m; j ++) { // cout << mp[i][j] << " "; // } // } memset(d, 0, sizeof(d)); bfs(yx, yy); bfs(mx, my); int ans = INF; for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { if(mp[i][j] == 3) { if(d[i][j] != 0) { //要注意还有可能有的KFC不能到达 ans = min(ans, d[i][j]); } } } } printf("%d\n", ans * 11); } return 0; }
HDU - 2612 - Find a way (BFS)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。