1. 程式人生 > 實用技巧 >HDU2612 Find a way

HDU2612 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.

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


題意:
終於在杭州經過一年的學習,yifenfei終於抵達故鄉寧波。離開寧波一年後,yifenfei有很多人見面。特別是好朋友Merceki。 Yifenfei的家
在鄉下,而Merceki的家在城市中心。因此,yifengei與Merceki安排了在肯德基會面。寧波有很多肯德基,他們想選擇一種讓總時間最短
的肯德基。 現在給您一張寧波地圖,yifenfei和Merceki都可以通過成本11分鐘左右上下移動到相鄰的道路。

輸入 :輸入包含多個測試用例。 每個測試用例包括前兩個整數n,m。 (2 <= n,m <= 200)。 接下來的n行,每行包含m個字元。
“ Y”表示yifenfei的初始位置。 ``M''表示Merceki的初始位置。 “#”禁止道路; ‘。’可通行道路。 “ @” KCF

輸出 : 對於每個測試用例輸出,yifenfei和Merceki到達肯德基之一的最短總時間。您可以確保始終有一個肯德基可以讓他們見面。

思路:用佇列實現bfs很方便 ,只要找到兩個人分別到不同kcf的步數再分別儲存起來 ,最後找出最小的。

 1 #include<stdio.h>
 2 #include<queue>
 3 #include<cstring> 
 4 #include<algorithm>
 5 using namespace std;
 6 int next[4][2]={1,0,-1,0,0,1,0,-1};//方向陣列 
 7 int book[210][210]={0};  //標記陣列 
 8 char map[210][210];      //地圖 
 9 int num1[210][210],num2[210][210];   //記錄兩人分別到不同kcf的步數 
10 int n,m;
11 struct node{
12     int a,b,step;
13 };
14 
15 void bfs(int p,int x,int y){
16     memset(book,0,sizeof(book));
17     
18     book[x][y]=1;
19     node point,npoint;
20     
21     queue<node> q;
22     point.a=x;
23     point.b=y;
24     point.step=0;
25     q.push(point);
26     while(!q.empty()){      //佇列為空即遍歷完成 
27         point=q.front();
28         q.pop();
29         if(map[point.a][point.b]=='@'){   //兩個人步數的處理 
30             if(p==1){
31                 num1[point.a][point.b]=point.step;    
32             }
33             if(p==2){
34                 num2[point.a][point.b]=point.step;
35             }        
36         }
37     
38         for(int i=0;i<4;i++){   //四個方向的遍歷 
39         npoint.a=point.a+next[i][0];
40         npoint.b=point.b+next[i][1];
41             if(npoint.a>=1&&npoint.a<=n&&npoint.b>=1&&npoint.b<=m&&map[npoint.a][npoint.b]!='#'&&book[npoint.a][npoint.b]==0){
42                 //臨界值,是否已經遍歷 ,該路是否通行 
43                 book[npoint.a][npoint.b]=1;
44                 npoint.step=point.step+1;
45                 q.push(npoint);
46             }
47         }
48     }
49 }
50 int main(){
51     while(~scanf("%d%d",&n,&m)){
52         int a1,a2,b1,b2;
53         int num;
54         getchar();
55         for(int i=1;i<=n;i++){
56             for(int j=1;j<=m;j++){
57                 scanf("%c",&map[i][j]);
58                 if(map[i][j]=='Y'){   //找到並記錄位置 
59                     a1=i;
60                     b1=j;
61                 }
62                 else if(map[i][j]=='M'){   
63                     a2=i;
64                     b2=j;
65                 }
66             }
67             getchar();
68         }
69         //將陣列初始化  
70         memset(num1,0x3f3f3f3f,sizeof(num1));
71         memset(num2,0x3f3f3f3f,sizeof(num2));
72         num=0x3f3f3f3f;
73         bfs(1,a1,b1);
74         bfs(2,a2,b2);
75         //通過遍歷兩個num陣列  來找出和最小的 即為最佳路徑 
76         for(int i=1;i<=n;i++){
77             for(int j=1;j<=m;j++){
78                 if(map[i][j]=='@'){
79                     num=min(num,num1[i][j]+num2[i][j]);
80                 }
81             }
82         }
83         printf("%d\n",num*11); 
84     }
85     return 0;
86 }