Find a Way (雙bfs)

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

InputThe 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 
OutputFor 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 Inputexpress

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

Sample Output數組

66
88
66

題目大意:ide

有兩我的(用Y和M表示)要到同一個KFC(用@表示),且存在多個KFC,找一個KFC使得兩人到改KFC的總時間最短,輸出該最短期。spa

思路:code

用兩次bfs分別求出Y和M到各個KFC的最短期,能夠開一個數組儲存每一個KFC的時間,最後相加二者的時間並取最小值。blog

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<queue>
  5 #define MAX 0x3f3f3f3f
  6 using namespace std;
  7 char map[210][210];  //用來儲存原始地圖
  8 struct node
  9 {
 10     int x;
 11     int y;
 12     int step;
 13 };
 14 int n, m;
 15 int T[210][210];  //記錄時間
 16 int nextd[4][2] = { 0,1,0,-1,1,0,-1,0 };  //4個運動方向
 17 void bfs(int x, int y)
 18 {
 19     bool vis[210][210];   //記錄是否走過該點,true爲走過,false爲未通過。
 20     memset(vis, false, sizeof(vis));
 21     queue<node>q;        //常規的bfs
 22     node t, s;
 23     vis[x][y] = true;
 24     s.x = x;
 25     s.y = y;
 26     s.step = 0;
 27     q.push(s);
 28     while (!q.empty())
 29     {
 30         s = q.front();
 31         q.pop();
 32         if (map[s.x][s.y] == '@')
 33         {
 34             if (T[s.x][s.y] == MAX)    //判斷Y是否已經到改@
 35                 T[s.x][s.y] = s.step;
 36             else
 37                 T[s.x][s.y] += s.step;
 38         }
 39         for (int i = 0; i < 4; i++)
 40         {
 41             t.x = s.x + nextd[i][0];
 42             t.y = s.y + nextd[i][1];
 43             t.step = s.step + 1;
 44             if (t.x < 0 || t.x >= n || t.y < 0 || t.y >= m) continue;  //出界
 45             if (vis[t.x][t.y]) continue;      //到過該點
 46             if (map[t.x][t.y] == '#') continue;    //不可通過的點
 47             vis[t.x][t.y] = true;
 48             q.push(t);
 49         }
 50     }
 51 }
 52  
 53 int main()
 54 {
 55     node M, Y;
 56     int i, j;
 57     while (~scanf("%d %d", &n, &m))
 58     {
 59         memset(T, 0x3f, sizeof(T));
 60         for (i = 0; i < n; i++) scanf("%s", map[i]);
 61         for (i = 0; i < n; i++)
 62         {
 63             for (j = 0; j < m; j++)
 64             {
 65                 if (map[i][j] == 'M')
 66                 {
 67                     M.x = i;
 68                     M.y = j;
 69                 }
 70                 if (map[i][j] == 'Y')
 71                 {
 72                     Y.x = i;
 73                     Y.y = j;
 74                 }
 75             }
 76         }
 77         /*for(i=0;i<n;i++)
 78         {
 79             for(j=0;j<m;j++)
 80             {
 81                 printf("%d ",T[i][j]);
 82             }printf("\n");
 83         } */
 84         bfs(Y.x, Y.y);
 85         /*for(i=0;i<n;i++)
 86         {
 87             for(j=0;j<m;j++)
 88             {
 89                 printf("%d ",T[i][j]);
 90             }printf("\n");
 91         } */
 92         bfs(M.x, M.y);
 93         /*for(i=0;i<n;i++)
 94         {
 95             for(j=0;j<m;j++)
 96             {
 97                 printf("%d ",T[i][j]);
 98             }printf("\n");
 99         } */
100         int min = MAX;
101         for (i = 0; i < n; i++)        //找到最小的總時間
102         {
103             for (j = 0; j < m; j++)
104             {
105                 if (min >T[i][j]) 
106                 min = T[i][j];
107             }
108         }
109         printf("%d\n", min*11);
110     }
111     return 0;
112 }
不給看
相關文章
相關標籤/搜索