POJ 1383 Labyrinth "Labyrinth bfs+ Tree Diameter"

Source: Internet
Author: User

Labyrinth
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 4004 Accepted: 1504

Description

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

3####.### #7 6########.#.####.#.####.#.#.##.....########

Sample Output

Maximum rope length is 0.Maximum rope length is 8.

Test instructions: A piece of rope, can be used in the maze, the rope can be from any point in the maze to any other point, the minimum length of the rope
The best way to find the longest path in the maze, using the diameter of the tree to find a method, two times BFS
#include <stdio.h> #include <string.h> #include <queue> #define MAX 1010using namespace Std;char map[ Max][max];bool vis[max][max];int n,m;int ans,sum,x1,y1,x2,y2;struct node{int x,y;int Ong;}; void Getmap () {int i,j;for (i=0;i<n;i++) scanf ("%s", Map[i]), for (i=0;i<n;i++) for (j=0;j<m;j++) {if (map[i][j    ]== '. ')    {x1=i;y1=j; return;}}} BOOL Judge (int c,int R) {if (c>=0&&c<n&&r>=0&&r<m&&map[c][r]!= ' # ' & &AMP;!VIS[C][R]) return True;return false;} void BFs (int x1,int y1) {int i,j;int move[4][2]={0,1,0,-1,1,0,-1,0};memset (vis,false,sizeof (VIS)); node Beg,end;queue <node>q;beg.x=x1;beg.y=y1;beg.ong=0;vis[x1][y1]=true;q.push (Beg); while (!q.empty ()) {End=q.front (); Q.pop () ; for (i=0;i<4;i++) {beg.x=end.x+move[i][0];beg.y=end.y+move[i][1];if (judge (Beg.x,beg.y)) {vis[beg.x][beg.y]= True;beg.ong=end.ong+1;if (Ans<beg.ong) {ans=beg.ong;x2=beg.x;y2=beg.y;} Q.push (Beg);}}} int main () {int t;scanf ("%d", &t), while (t--) {GetchaR (); scanf ("%d%d", &m,&n); Ans=0;getmap (); BFs (x1,y1); BFS (x2,y2);p rintf ("Maximum rope length is%d.\n", ans);} return 0;}

  

POJ 1383 Labyrinth "Labyrinth bfs+ Tree Diameter"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.