HDU 2612.Find a way

Find a way
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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. 

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
 
 
有两个起点的BFS,每个KFS是一个出口,分别计算出每个出口距离两个入口的距离,计算其最小值
 
要特别注意:KFC有可能不能到达
 
  1 /*
  2 By:OhYee
  3 Github:OhYee
  4 Email:oyohyee@oyohyee.com
  5 Blog:http://www.cnblogs.com/ohyee/
  6 
  7 かしこいかわいい?
  8 エリーチカ!
  9 要写出来Хорошо的代码哦~
 10 */
 11 
 12 #include <cstdio>
 13 #include <algorithm>
 14 #include <cstring>
 15 #include <cmath>
 16 #include <string>
 17 #include <iostream>
 18 #include <vector>
 19 #include <list>
 20 #include <queue>
 21 #include <stack>
 22 #include <map>
 23 using namespace std;
 24 
 25 //DEBUG MODE
 26 #define debug 0
 27 
 28 //循环
 29 #define REP(n) for(int o=0;o<n;o++)
 30 
 31 const int maxn = 205;
 32 int n,m;
 33 char Map[maxn][maxn];
 34 const int delta[] = {1,-1,0,0};
 35 
 36 struct point {
 37     int x,y;
 38     point() {
 39         x = y = -1;
 40     }
 41     point(int a,int b) {
 42         x = a;
 43         y = b;
 44     }
 45 };
 46 int num;
 47 
 48 
 49 int BFS(point s,int (&dis)[maxn][maxn]) {
 50     memset(dis,-1,sizeof(dis));
 51     queue<point> Q;
 52 
 53     Q.push(s);
 54     dis[s.x][s.y] = 0;
 55 
 56     while (!Q.empty()) {
 57         int x = Q.front().x;
 58         int y = Q.front().y;
 59         Q.pop();
 60 
 61         REP(4) {
 62             int xx = x + delta[o];
 63             int yy = y + delta[3 - o];
 64 
 65             if (xx < 0 || xx >= n || yy < 0 || yy >= m)
 66                 continue;
 67             if (Map[xx][yy] == '#')
 68                 continue;
 69             if (dis[xx][yy] == -1) {
 70                 dis[xx][yy] = dis[x][y] + 1;
 71                 Q.push(point(xx,yy));
 72             }
 73         }
 74     }
 75     return -1;
 76 }
 77 
 78 bool Do() {
 79     if (scanf("%d%d",&n,&m) == EOF)
 80         return false;
 81     point s1,s2;
 82     num = 0;
 83     point v[maxn*maxn];
 84     for (int i = 0;i < n;i++)
 85         for (int j = 0;j < m;j++) {
 86             scanf("\n%c",&Map[i][j]);
 87             if (Map[i][j] == 'Y')
 88                 s1 = point(i,j);
 89             if (Map[i][j] == 'M')
 90                 s2 = point(i,j);
 91             if (Map[i][j] == '@') 
 92                 v[num++] = point(i,j);
 93         }
 94 
 95     int dis1[maxn][maxn];
 96     int dis2[maxn][maxn];
 97 
 98     BFS(s1,dis1);
 99     BFS(s2,dis2);
100 
101     int Min = 100000;
102     for (int i = 0;i < num;i++)
103         if(dis1[v[i].x][v[i].y]!=-1&& dis2[v[i].x][v[i].y]!=-1)
104         Min = min(Min,dis1[v[i].x][v[i].y]+ dis2[v[i].x][v[i].y]);
105 
106     printf("%d\n",Min * 11);
107 
108     return true;
109 }
110 
111 int main() {
112     while (Do());
113     return 0;
114 }

 

posted @ 2016-04-24 21:15  OhYee  阅读(119)  评论(0编辑  收藏  举报