前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu-2612-Find a way(双 bfs)

hdu-2612-Find a way(双 bfs)

作者头像
Cell
发布2022-02-25 14:37:38
1320
发布2022-02-25 14:37:38
举报
文章被收录于专栏:Cell的前端专栏

Find a way

圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车。百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个。坤神:我要去左边的这个(因为离自己比较近 哈哈~)。瑞瑞:我要去右边的这个(因为离自己比较近 嘿嘿~) …….. 这对基佬闹矛盾了,开车有危险了! 为了不让他们去召唤师大峡谷坑人,riot 决定让他们去 X 召唤师大峡谷,保证他俩所走的路程和最短。每走一个点需要花费 11 分钟,输出他们一共花费多少时间(最短时间噢)

Input

多组测试数据

每组数据,开始一行 n,m (2<=n,m<=200)

接下来是个 n x m 的矩阵

‘Y’ 表示坤神所在的初始位置

‘M’ 表示瑞瑞所在的初始位置

‘#’ 该点禁止通行

‘.’ 该点可通行

‘@’ 召唤师大峡谷

Output

每组测试数据,输出坤神和瑞瑞到达同一个召唤师大峡谷所花费的最短时间。

Sample Input

代码语言:javascript
复制
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
@..M.
`#...#`

Sample Output

代码语言:javascript
复制
66
88
66

Hint

对于第一组样例,坤神和瑞瑞去矩阵(4,1) 这个召唤师大峡谷,耗费的时间为 3 * 11 + 3 * 11 = 66, 去矩阵(1,4)这个召唤师大峡谷,耗费的时间为 5 * 11 + 3 * 11 = 88 。所以,最终答案:66。思路参考

写代码总是好粗心!!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

#include <bits/stdc++.h> #define inf 0x3f3f3f3f //acm 中“无穷大”的一般定义 using namespace std; const int M=202; char mp[M][M]; //地图 int a[M][M],b[M][M]; bool vis[M][M]; //标记数组 int n,m; int ans; struct node { int x,y,step; }; void init() //初始化函数 { ans=inf; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { a[i][j]=inf; b[i][j]=inf; } } void bfs(int x,int y,bool flag){ int dir[4][2]={-1,0,1,0,0,1,0,-1}; node u,v; queue<node> q; //初始化队列第一个元素 u.x=x; u.y=y; u.step=0; vis[x][y]=true; q.push(u); while(!q.empty()){ u=q.front(); q.pop(); if(mp[u.x][u.y]=='@'){ if(!flag) a[u.x][u.y]=u.step; else b[u.x][u.y]=u.step; } for(int i=0;i<4;i++){ int tx=u.x+dir[i][0]; int ty=u.y+dir[i][1]; if(tx>=0&&ty>=0&&tx<n&&ty<m&&!vis[tx][ty]&&mp[tx][ty]!='#'){//注意@和 M,Y 也是可以走的。 v.x=tx; //每次写搜索都忘记 vis!!!! v.y=ty; vis[tx][ty]=true; //我总是忘记。 v.step=u.step+1; q.push(v); } } } } int main() { while(~scanf("%d%d",&n,&m)) { init(); for(int i=0; i<n; i++) scanf("%s",mp[i]); for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(mp[i][j]=='Y') { memset(vis,false,sizeof(vis)); bfs(i,j,false); } if(mp[i][j]=='M') { memset(vis,false,sizeof(vis)); //记得再次初始化标记数组 bfs(i,j,true); } } for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(mp[i][j]=='@') ans=min(ans,a[i][j]+b[i][j]); printf("%d\n",ans*11); } return 0; }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Find a way
  • Input
  • Output
  • Sample Input
  • Sample Output
  • Hint
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档