HDU-2612
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14087 Accepted Submission(s): 4485
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
题目大意:y和m要去肯德基聚餐,图中有多个kfc,他们要选的那个kfc必须到彼此的所用时间之和最小,问 最少需要多少时间(这里一格代表了11分钟)。
解题思路:将y到每点的时间和m到每点的时间bfs跑完存到二维数组dist中,打好表后扫地图,如果扫到@的话算下时间之和,更新min。
#include<bits/stdc++.h> using namespace std; struct node{ int x,y,step; }; char mp[210][210]; int yx,yy,mx,my,n,m; int dir[4][2]={1,0,-1,0,0,1,0,-1}; bool vis[210][210]; int dist1[210][210],dist2[210][210]; queue<node> Q; void bfs(node p,int dist[][210]){ node q; memset(dist,0,sizeof(dist)); memset(vis,0,sizeof(vis)); Q.push(p); while(!Q.empty()){ p=Q.front(); Q.pop(); for(int i=0;i<4;i++){ q.x=p.x+dir[i][0]; q.y=p.y+dir[i][1]; q.step=p.step+1; if(q.x<n&&q.x>=0&&q.y<m&&q.y>=0&&mp[q.x][q.y]!='#'&&vis[q.x][q.y]==0){ vis[q.x][q.y]=1; dist[q.x][q.y]=q.step; Q.push(q); } } } } int main(){ while(cin>>n>>m){ for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>mp[i][j]; if(mp[i][j]=='Y'){ yx=i; yy=j; } if(mp[i][j]=='M'){ mx=i; my=j; } } } int min=999999999; node p; p.x=yx; p.y=yy; p.step=0; bfs(p,dist1); p.x=mx; p.y=my; p.step=0; bfs(p,dist2); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(mp[i][j]=='@'){ if(dist1[i][j]!=0&&dist2[i][j]!=0) if(dist1[i][j]+dist2[i][j]<min) min=dist1[i][j]+dist2[i][j]; } } } cout<<min*11<<endl; } return 0; }