1145. Rope in the Labyrinthtime limit:0.5 second
Memory limit:64 MBA labyrinth with rectangular form and size
mX
Nis divided to square cells with sides ' length 1 by lines which is parallel with the labyrinth ' s sides. Each cell of the grid is either occupied or free. It is possible to move from one free cell to another free cells, share a common side with the cell. One cannot move beyond the labyrinth ' s borders. The Labyrinth is designed pretty specially:for any of the cells there is the one-from-one-to-move from one cell to the other. There is a hooks at each cell ' s center. In the labyrinth there is both special free cells, such so if you can connect the hooks of the those of both cells with a rope, The Labyrinth ' s secret door'll be automatically opened. The problem is to prepare a shortest rope so can guarantee, you always can connect the hooks of the those of both cells with the Prepared rope regardless their position in the labyrinth. Inputthe First line contains integers
Nand
m(3≤
N,
m≤820). The next lines describe the labyrinth. Each of the next
mLines contains
NCharacters. Each character are either "#" or ".", with "#" indicating a occupied cell, and "." indicating a free cell. Outputprint out of the "measured in the number of cells" of the required rope. Sample
input |
Output |
7 6########.#.####.#.####.#.#.##.....######## |
8 |
Tags:Graph theory()difficulty:425 Test Instructions: It is possible to walk, #不可以走. All the. Make up a tree and ask the diameter of the tree. Analysis: Finding the diameter of the tree. BFS can be.
1 /**2 Create by Yzx-stupidboy3 */4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <cmath>8#include <deque>9#include <vector>Ten#include <queue> One#include <iostream> A#include <algorithm> -#include <map> -#include <Set> the#include <ctime> -#include <iomanip> - using namespacestd; -typedefLong LongLL; +typedefDoubleDB; - #defineMIT (2147483647) + #defineINF (1000000001) A #defineMLL (1000000000000000001LL) at #defineSZ (x) ((int) (x). Size ()) - #defineCLR (x, y) memset (x, y, sizeof (x)) - #definePUF Push_front - #definePub push_back - #definePOF Pop_front - #definePOB pop_back in #defineFT first - #defineSD Second to #defineMk Make_pair + -InlineintGetint () the { * intRet =0; $ CharCh =' ';Panax Notoginseng BOOLFlag =0; - while(! (Ch >='0'&& Ch <='9')) the { + if(Ch = ='-') Flag ^=1; ACh =GetChar (); the } + while(Ch >='0'&& Ch <='9') - { $RET = RET *Ten+ Ch-'0'; $Ch =GetChar (); - } - returnFlag? -Ret:ret; the } - Wuyi Const intN =1010; the Const intDx[] = {-1,0,1,0}, dy[] = {0, -1,0,1}; - intN, M; Wu CharGraph[n][n]; - intDp[n][n]; Aboutqueue<pair<int,int> >que; $ -InlinevoidInput () - { -scanf"%d%d", &m, &n); A for(inti =0; I < n; i++) scanf ("%s", Graph[i]); + } the -InlineBOOLCheck (intXinty) $ { the if(X <0|| Y <0|| X >= N | | Y >= m)return 0; the if(Graph[x][y]! ='.')return 0; the return 1; the } - inInlinevoidBfs (intSxintSy) the { the for(inti =0; I < n; i++) About for(intj =0; J < M; J + +) theDP[I][J] =INF; the Que.push (MK (SX, SY)); theDp[sx][sy] =0; + while(sz (que)) - { the intUX = Que.front (). ft, Uy =Que.front (). SD;Bayi Que.pop (); the for(intt =0; T <4; t++) the { - intVX = UX + dx[t], vy = Uy +Dy[t]; - if(Check (VX, VY) && Dp[vx][vy] > Dp[ux][uy] +1) the { theDp[vx][vy] = Dp[ux][uy] +1; the Que.push (MK (VX, VY)); the } - } the } the } the 94InlinevoidGetmax (int&PX,int&py) the { the intMX =-INF; the for(inti =0; I < n; i++)98 for(intj =0; J < M; J + +) About if(MX < dp[i][j] && Dp[i][j] <INF) - {101MX =Dp[i][j];102px = i, py =J;103 }104 } the 106InlinevoidSolve ()107 {108 BOOLFlag =0;109 for(inti =0; I < n &&!flag; i++) the for(intj =0; J < m &&!flag; J + +)111 if(Graph[i][j] = ='.') the {113 Bfs (i, j); theFlag =1; the } the 117 intpx, py;118 getmax (px, py);119 Bfs (px, py); - 121 getmax (px, py);122printf"%d\n", Dp[px][py]);123 }124 the intMain ()126 {127Freopen ("a.in","R", stdin); - Input ();129 Solve (); the return 0;131}
View Code
Ural 1145. Rope in the Labyrinth