HDU - 2612 Find a way
题目:
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
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
分析:
这个题跟前面那个Fire!那个题很像,都是多起点BFS问题,但是之前那个题一个火走过的地方另一个火就不用走了,所以只用一套棋盘BFS一次就行了。
但是这次这个题一个人走过的格子另一个人也能走,所以得用两套棋盘分别记录两个人走过每个格子的时间
代码:
1 #include<iostream> 2 #include<queue> 3 #include<string.h> 4 using namespace std; 5 6 struct Pos{ 7 int x,y; 8 int time; 9 }; 10 11 int n=0,m=0; 12 int x_1,y_1,x_2,y_2; 13 char m1[200][200]; 14 int m2[200][200]={0}; //BFS棋盘 15 int m3[200][200]={0}; //暂存结果的棋盘 16 int m4[200][200]={0}; 17 18 queue<Pos> q; 19 int X[]={-1,1,0,0}; 20 int Y[]={0,0,-1,1}; 21 22 void BFS(int x0,int y0){ 23 Pos pos0; 24 pos0.time=0;pos0.x=x0;pos0.y=y0; 25 q.push(pos0); 26 while(!q.empty()){ 27 Pos p=q.front();q.pop(); 28 for(int i=0;i<4;i++){ 29 Pos pp; 30 pp.time=p.time+11; 31 pp.x=p.x+X[i]; 32 pp.y=p.y+Y[i]; 33 if(pp.x>=0&&pp.x<n&&pp.y>=0&&pp.y<m&&m2[pp.x][pp.y]==0){ 34 m2[pp.x][pp.y]=pp.time; 35 q.push(pp); 36 } 37 } 38 } 39 } 40 41 int main(){ 42 while(cin >>n>>m){ 43 for(int i=0;i<n;i++){ 44 for(int j=0;j<m;j++){ 45 cin >>m1[i][j]; 46 if(m1[i][j]=='#'){ 47 m2[i][j]=-1; 48 m3[i][j]=-1; 49 }else if(m1[i][j]=='Y'){ 50 m2[i][j]=1; 51 m3[i][j]=1; 52 x_1=i;y_1=j; 53 }else if(m1[i][j]=='M'){ 54 m2[i][j]=1; 55 m3[i][j]=1; 56 x_2=i;y_2=j; 57 }else{ 58 m2[i][j]=0; 59 m3[i][j]=0; 60 } 61 } 62 } 63 //BFS 64 BFS(x_1,y_1); 65 memcpy(m4,m2,sizeof(m2)); 66 memcpy(m2,m3,sizeof(m3)); 67 memcpy(m3,m4,sizeof(m4)); 68 BFS(x_2,y_2); 69 int min_time=999999; 70 for(int i=0;i<n;i++){ 71 for(int j=0;j<m;j++){ 72 if(m1[i][j]=='@'){ 73 if(m2[i][j]+m3[i][j]>0){ 74 if(m2[i][j]+m3[i][j]<min_time){ 75 min_time=m2[i][j]+m3[i][j]; 76 } 77 } 78 } 79 } 80 } 81 cout <<min_time<<endl; 82 for(int i=0;i<n;i++){ 83 for(int j=0;j<m;j++){ 84 m1[i][j]=0;m2[i][j]=0;m3[i][j]=0;m4[i][j]=0; 85 } 86 } 87 } 88 system("pause"); 89 return 0; 90 }


浙公网安备 33010602011771号