HDU 2612 Find a way(BFS)
2017-07-21 09:46
441 查看
Find a way
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
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
这道题本来的思路是遍历整张地图,以@为起点进行BFS,在所得结果中取最小值。
后来发现@的数量是未知的,可能有无穷多个,这样写肯定会超时。
就改成以Y,M为起点进行两次BFS,把结果记录到一个图中,图中不为0的最小值就是结果。
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<queue> using namespace std; char mp[222][222]; int res[222][222]; bool vis[222][222]; int n,m; const int inf=99999; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; struct node { int x,y,step; }; bool check(int x,int y) { if(x<n&&x>=0&&y<m&&y>=0) return 1; return 0; } void bfs(int x,int y) { node now,next; queue<node>q; now.x=x; now.y=y; now.step=0; vis[x][y]=1; q.push(now); memset(vis,0,sizeof(vis)); while(!q.empty()) { now=q.front(); q.pop(); if(mp[now.x][now.y]=='@') res[now.x][now.y]+=now.step; for(int i=0;i<4;i++) { int xx=now.x+dir[i][0]; int yy=now.y+dir[i][1]; if(check(xx,yy)&&mp[xx][yy]!='#'&&!vis[xx][yy]) { vis[xx][yy]=1; next.x=xx; next.y=yy; next.step=now.step+1; q.push(next); } } } } int main() { int yx,yy; int mx,my; while(cin>>n>>m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>mp[i][j]; } } memset(res,0,sizeof(res)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mp[i][j]=='Y') { yx=i; yy=j; } if(mp[i][j]=='M') { mx=i; my=j; } } } bfs(yx,yy); bfs(mx,my); int ans=inf; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(res[i][j]) { ans=min(ans,res[i][j]); } } } cout<<ans*11<<endl; } return 0; }
相关文章推荐
- hdu 2612 Find a way(BFS)
- HDU.2612 Find a way (BFS)
- HDU 2612 Find a way (两次bfs)
- BFS(最短路) HDU 2612 Find a way
- HDU 2612 Find a way BFS
- hdu 2612 Find a way(BFS)
- Find a way HDU - 2612 双向BFS
- hdu 2612 Find a way(双向bfs)
- HDU2612 Find a way —— BFS
- hdu2612 Find a way--BFS & 打表
- HDU 2612 Find a way(两次bfs)
- hdu 2612 Find a way ( 双bfs )
- 【HDU】-2612-Find a way(BFS,好)
- hdu 2612 Find a way (BFS)
- HDU - 2612 Find a way(BFS)
- HDU 2612 Find a way(双BFS) (N)
- HDU 2612 Find a way 多起点 多终点 BFS
- HDU 2612 -Find a way (注重细节BFS)
- HDU-OJ 杭电2612 Find a way 双层BFS
- HDU 2612 Find a way 简单BFS