Dogstime limit:2000/1000ms (java/other) Memory limit:32768/32768k (Java/other) total submission (s): 8 Accepted Sub Mission (s): 2font:times New Roman | Verdana | Georgiafont Size:←→problem Descriptionprairie 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). Inputthe input is divided to 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. Outputfor should just output a line which contains only one integer, which is the number of minimal lattices Tim must dig. Sample Input
6 6..X ... Xxx. X..... x.x ..... X..... x.x ... 3 56 30 0
Sample Output
3
Hinthint:three lattices Tim should dig: (2, 4), (3, 1), (6, 2). Source2009 multi-university Training Contest 1-host by Tju idea: Take priority queue, which point time less which point first processing
#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespacestd;intN,m,i,j,sx,sy,tx,ty;Charmp[1005][1005];BOOLvis[1005][1005];//int burst memory, with bool typestructnode{intX,y,ti; Node (intAintBintc) {x=a;y=b;ti=c;}};intdr[4][2]={{1,0},{-1,0},{0,1},{0,-1} };structcmp{BOOL operator() (node A,node b) {returnA.ti>B.ti; }};BOOLCheckintXinty) { if(x>=0&& x<n && y>=0&& y<m &&!vis[x][y])return 1; return 0;}intBFs () {priority_queue<node,vector<node>,cmp>Q; Q.push (Node (sx,sy,0)); Vis[sx][sy]=1; while(!Q.empty ()) {Node P=Q.top (); Q.pop (); for(intI=0;i<4; i++) { intxx=p.x+dr[i][0]; intyy=p.y+dr[i][1]; if(!check (XX,YY))Continue; VIS[XX][YY]=1; intTi=0; if(mp[xx][yy]=='X') ti=P.ti; Elseti=p.ti+1; Q.push (Node (xx,yy,ti)); if(Xx==tx && Yy==ty)returnti; } }}intMain () { while(SCANF ("%d%d",&n,&m)) {if(n==0&& m==0) Break;//ended with 0 0 for(i=0; i<n;i++) scanf ("%s",&Mp[i]); scanf ("%d%d",&sx,&Sy); scanf ("%d%d",&tx,&ty); SX--; Sy--; TX--; Ty--; memset (Vis,0,sizeof(VIS)); if(Sx==tx && sy==ty) printf ("0\n"); Elseprintf"%d\n", BFS ()); } return 0;}
HDU 2822 Dogs