首页 > 代码库 > 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): 4044 Accepted Submission(s): 1348
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
奋斗的年代
Recommend
yifenfei | We have carefully selected several similar problems for you: 2717 1254 1728 2102 1072
题意:
求M和Y到达同一个‘@’所需要的时间总和最小,每步需要时间11分钟。
题解:
两次BFS求M和Y到每个‘@’之间的最小距离,之后求相加的最小值。
CODE:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<set> #include<queue> #include<stack> #include<vector> #include<map> #define N 100010 #define Mod 10000007 #define lson l,mid,idx<<1 #define rson mid+1,r,idx<<1|1 #define lc idx<<1 #define rc idx<<1|1 const double EPS = 1e-11; const double PI = acos(-1.0); typedef long long ll; const int INF=1000010; using namespace std; struct node { int x,y; int num;//保存两人到@的最小值 int num2; }; vector<node>q; queue<node>que; int n,m; char mp[222][222]; bool vis[222][222]; int len,Min,l; int Mx,My,Yx,Yy; int xx[4]= {-1,0,1,0}; int yy[4]= {0,1,0,-1}; void bfs(int x,int y) { memset(vis,0,sizeof vis); while(que.size()) que.pop(); node t,tt; t.x=x,t.y=y; t.num=0; que.push(t); int s=0; while(que.size()) { t=que.front(); que.pop(); if(mp[t.x][t.y]=='@') { for(int i=0; i<l; i++)//找到@ { if(q[i].x==t.x&&q[i].y==t.y) { q[i].num=min(t.num,q[i].num); break; } } } for(int i=0; i<4; i++) { tt.x=t.x+xx[i]; tt.y=t.y+yy[i]; tt.num=t.num+1; if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&mp[tt.x][tt.y]!='#'&&!vis[tt.x][tt.y]) { que.push(tt); vis[tt.x][tt.y]=1; } } } } int main() { //freopen("test.in","r",stdin); while(cin>>n>>m) { len=0; q.clear(); node a; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { cin>>mp[i][j]; if(mp[i][j]=='@') { a.x=i,a.y=j,a.num=INF,a.num2=0; q.push_back(a); } if(mp[i][j]=='Y') { Yx=i,Yy=j; } if(mp[i][j]=='M') { Mx=i,My=j; } } l=q.size(); bfs(Mx,My); for(int i=0; i<l; i++)//把num的值转给num2,num重新复制无穷大 { q[i].num2=q[i].num; q[i].num=INF; } bfs(Yx,Yy); Min=INF; for(int i=0; i<l; i++) { if(q[i].num2+q[i].num<Min) Min=q[i].num+q[i].num2; } cout<<Min*11<<endl; } return 0; }
Hdu 2612 Find a way(bfs)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。