Topic Connection
http://poj.org/problem?id=1383
Labyrinthdescription
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided to square blocks, each of which them either filled by rock, or free. There is also a little hooks on the ' floor ' of the center of the every free block. The ACM has found that, the hooks must is connected by a rope, runs through the hooks in every block on the PAT h between the connected ones. When the rope is fastened, a secret door opens. The problem is the and we don't know which hooks to connect. That means also, the neccessary length of the rope is unknown. Your task is to determine the maximum length of the rope we could need for a given labyrinth.
Input
The input consists of T test cases. The number of them (T) is given in the first line of the input file. Each test case begins with a line containing the integers C and R (3 <= c,r <=) indicating the number of column s and rows. Then exactly R lines follow, each containing C characters. These characters specify the labyrinth. Each of the them is either a hash mark (#) or a period (.). Hash marks represent rocks, periods is free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks is blocks sharing a common side. We cannot walk diagonally and we cannot step out of the labyrinth.
The Labyrinth is designed in such a by that there is exactly one path between any and free blocks. Consequently, if we find the proper hooks to connect, it's easy-to-find the right path connecting them.
Output
Your program must print exactly one line of output for each test case. The line must contain the sentence "Maximum rope length is x." Where Xis the length of the longest path between any and FR EE blocks, measured in blocks.
Sample Input
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
Sample Output
Maximum rope length is 0.
Maximum rope length is 8.
The diameter of the tree.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <map>using std::map;using std::min;using std:: Sort;using std::p air;using std::queue;using std::vector;using std::multimap;using std::make_pair; #define PB (E) push_ Back (e) #define SZ (c) (int.) (c). Size () #define MP (A, b) Make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) __typeof ((c). Begin ()) #define CLS (arr, Val) memset (arr, Val, sizeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #define REP (i, n) for (int i = 0; i < (int.) n; i++) #define TR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); + + i) const int N = 1010;const int INF = 0x3f3f3f3f;struct P {int x, y, S; P (int i = 0, int j = 0, int k = 0): x (i), Y (j), S (k) {}};int H, w;char g[n][n];bool vis[n][n];const int dx[] = {0, 0,-1, 1}, dy[] = {-1, 1, 0, 0}; P BFS (int sx, int sy) {p res; Queue<p> Q; Q.push (P (SX, Sy)); CLS (Vis, false); Vis[sx][sy] = true; while (!q.empty ()) {P t = Q.front (); Q.pop (); if (T.s > RES.S) {res = P (t.x, T.y, T.S); } Rep (I, 4) {int x = Dx[i] + t.x, y = dy[i] + t.y; if (x < 0 | | x >= H | | y < 0 | | y >= W) continue; if (Vis[x][y] | | | ' # ' = = G[x][y]) continue; Vis[x][y] = true; Q.push (P (x, y, T.S + 1)); }} return res;} void Solve () {int SX, SY; Rep (i, H) {scanf ("%s", G[i]); Rep (J, W) {if (g[i][j] = = '. ') SX = i, sy = j; }} P res = BFS (SX, SY); res = BFS (res.x, RES.Y); printf ("Maximum rope length is%d.\n", RES.S);} int main () {#ifdef LOCAL freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w+", stdout); #endif int t; scanf ("%d", &t); while (t--) {scanf ("%d%d", &w, &h); Solve (); } return 0;}
Poj 1383 Labyrinth