Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=2822
Dogsdescription
Prairie Dog comes again! Someday One little prairie dog Tim wants to visit one of its friends on the farmland, but he's as lazy as his friend (who Required Tim to come-to-he place instead of going to Tim's), so he turn-to-you-to-point-how could him dig a s less as he could.
We know the farmland is divided to a grid, and some of the lattices form houses, where many little dogs live in. If the lattices connect to each and all of the they belong to the same house. Then the little Tim start from his home located at (x0, y0) on Aim at his friend's home (x1, y1). During the journey, he must walk through a lot of lattices, when in a house he can just walk through without digging, but He must dig some distance to reach another house. The farmland would be as big as the +, and the up left corner is labeled as (1, 1).
Input
The input is divided into blocks. The first line is each block contains and integers:the length m of the farmland, the width N of the farmland (m, n≤1000 ). The next lines contain m rows and each row has n letters, with an ' X ' stands for the lattices of house, and '. ' Stands for T He empty land. The following lines is the start and end places ' coordinates, we guarantee that they be located at ' X '. There'll be a blank line between every test case. The block where both numbers in the first line is equal to zero denotes the end of the input.
Output
Should just output a line which contains only one integer, which is the number of minimal lattices Tim m UST Dig.
Sample Input
6 6
.. X...
Xxx. X.
.... X.
X.....
X.....
x.x ...
3 5
6 3
0 0
Sample Output
3
Walk ' X ' without taking the time to go '. ' Time is 1
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <queue>8#include <map>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingStd::map; the usingstd::p air; - usingstd::vector; - usingStd::multimap; - usingstd::p riority_queue; + #definePB (E) push_back (e) - #defineSZ (c) (int) (c). Size () + #defineMP (A, b) Make_pair (A, B) A #defineAll (c) (c). Begin (), (c). End () at #defineITER (c) Decltype ((c). Begin ()) - #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intN =1010; intypedef unsignedLong Longull; - Const intDx[] = {0,0, -1,1}, dy[] = {-1,1,0,0 }; to BOOLVis[n][n]; + CharRec[n][n]; - intm, N, Sx, Sy, Dx, Dy; the structNode { * intx, y, S; $Node (inti =0,intj =0,intK =0): X (i), Y (j), S (k) {}Panax Notoginseng BOOL operator< (ConstNode &a)Const { - returns >A.S; the } + }; A voidBFs () { theCLS (Vis,false); +Priority_queue<node>que; -Que.push (Sx, Sy,0)); $Vis[sx][sy] =true; $ while(!Que.empty ()) { -Node tmp =que.top (); Que.pop (); - if(tmp.x = = Dx && tmp.y = = Dy) {printf ("%d\n", TMP.S);return; } theRep (I,4) { - intNX = tmp.x + dx[i], NY = tmp.y +Dy[i];Wuyi if(NX <0|| NX >= m | | NY <0|| NY >= N | | Vis[nx][ny])Continue; the if(Rec[nx][ny] = ='X') Que.push (Node (NX, NY, TMP.S)); - ElseQue.push (Node (NX, NY, TMP.S +1)); WuVis[nx][ny] =true; - } About } $ } - intMain () { - #ifdef LOCAL -Freopen ("In.txt","R", stdin); AFreopen ("OUT.txt","w+", stdout); + #endif the while(~SCANF ("%d%d", &m, &n) && m +N) { -Rep (i, M) scanf ("%s", Rec[i]); $scanf"%d %d%d%d", &SX, &sy, &DX, &Dy); theSx--, Sy--, Dx--, dy--; the BFS (); the } the return 0; -}
View Code
HDU 2822 Dogs