博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 2612 - Find a way (BFS)
阅读量:4648 次
发布时间:2019-06-09

本文共 3086 字,大约阅读时间需要 10 分钟。

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.
 

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
 

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 #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long#define INF 0x7fffffffusing 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
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;}

转载于:https://www.cnblogs.com/blfbuaa/p/6759011.html

你可能感兴趣的文章
python基础七之copy
查看>>
[大数据可视化]-saiku的源码包Bulid常见问题和jar包
查看>>
计算机科学速成课18:操作系统
查看>>
钱去那儿了
查看>>
iis windows phpstudy安装redis扩展
查看>>
jquery学习之1.10-小练习-选中下拉框内容并显示
查看>>
Qt5.6.0+OpenGL 纹理贴图首战告捷
查看>>
Docker Compose 容器编排
查看>>
PS:切图
查看>>
Spring Boot学习笔记——Spring Boot与ActiveMQ的集成
查看>>
Set接口
查看>>
Hibernate初探之单表映射——jar包的导入
查看>>
ElementUI使用问题记录:设置路由+iconfont图标+自定义表单验证
查看>>
[C#] 谈谈异步编程async await
查看>>
【转】测试人员职业规划
查看>>
思科交换机的初始配置(使用telnet登录)
查看>>
vim
查看>>
【学习笔记】APP测试基本流程及测试要点
查看>>
区间DP——入门
查看>>
SAS中修改一个表为编辑模式的时候不成功并给出警告的原因及解决办法
查看>>