Find A-to Time
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U Submit Status Practice HDU 2612
Description
Pass a year learning in Hangzhou, Yifenfei arrival hometown Ningbo at finally. Leave Ningbo One year, Yifenfei has many people to meet. Especially a good friend Merceki.
Yifenfei's home is on the countryside, but Merceki's home is in the center of the city. So Yifenfei made arrangements and Merceki to meet at a KFC. There is many KFC in Ningbo, they want to choose one and the total time to it is most smallest.
Now give your a Ningbo map, Both Yifenfei and Merceki can move up, down, left, right to the adjacent road by cost one minute S.
Input
The input contains multiple test cases.
Each test case include, first, 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, both Yifenfei and Merceki to arrival one of the KFC. Sure there is always has a KFC that can let them meet.
Sample Input
4 4y.#@.....#. @.. M4 4y.#@.....#. @#. M5 5[email protected].#....#...@. m.#...#
Sample Output
668866
There are two starting points of the BFS, each kfs is an exit, calculate the distance of each exit two inlet, calculate its minimum value to pay special attention to:
KFC may not be able to reach
1 /*2 By:ohyee3 Github:ohyee4 Email:[email protected]5 Blog:http://www.cnblogs.com/ohyee/6 7 かしこいかわいい? 8 エリーチカ! 9 to write out the хорошо code OH ~Ten */ One A#include <cstdio> -#include <algorithm> -#include <cstring> the#include <cmath> -#include <string> -#include <iostream> -#include <vector> +#include <list> -#include <queue> +#include <stack> A#include <map> at using namespacestd; - - //DEBUG MODE - #defineDebug 0 - - //Loops in #defineREP (n) for (int o=0;o<n;o++) - to Const intMAXN =205; + intn,m; - CharMAP[MAXN][MAXN]; the Const intDelta[] = {1,-1,0,0}; * $ structPoint {Panax Notoginseng intx, y; - Point () { thex = y =-1; + } APointintAintb) { thex =A; +y =b; - } $ }; $ intnum; - - the intBFS (Point S,int(&dis) [MAXN] [MAXN]) { -memset (dis,-1,sizeof(DIS));WuyiQueue<point>Q; the - Q.push (s); WuDIS[S.X][S.Y] =0; - About while(!Q.empty ()) { $ intx =Q.front (). x; - inty =Q.front (). Y; - Q.pop (); - AREP (4) { + intxx = x +Delta[o]; the intyy = y + delta[3-O]; - $ if(XX <0|| XX >= N | | yy <0|| YY >=m) the Continue; the if(Map[xx][yy] = ='#') the Continue; the if(Dis[xx][yy] = =-1) { -DIS[XX][YY] = Dis[x][y] +1; in Q.push (Point (Xx,yy)); the } the } About } the return-1; the } the + BOOLDo () { - if(SCANF ("%d%d", &n,&m) = =EOF) the return false;Bayi Point s1,s2; thenum =0; thePoint v[maxn*MAXN]; - for(inti =0; I < n;i++) - for(intj =0; J < m;j++) { thescanf"\n%c",&map[i][j]); the if(Map[i][j] = ='Y') theS1 =Point (i,j); the if(Map[i][j] = ='M') -S2 =Point (i,j); the if(Map[i][j] = ='@') thev[num++] =Point (i,j); the }94 the intDIS1[MAXN][MAXN]; the intDIS2[MAXN][MAXN]; the 98 BFS (S1,DIS1); About BFS (S2,DIS2); - 101 intMin =100000;102 for(inti =0; I < num;i++)103 if(dis1[v[i].x][v[i].y]!=-1&& dis2[v[i].x][v[i].y]!=-1)104min = min (min,dis1[v[i].x][v[i].y]+dis2[v[i].x][v[i].y]); the 106printf"%d\n", Min * One);107 108 return true;109 } the 111 intMain () { the while(Do ());113 return 0; the}
HDU 2612.Find A