POJ 1383 Labyrinth(BFS 樹的直徑)

Labyrinth

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
發張圖,壓壓驚。


Description

The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of
 them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. The ACM have found that
 two of the hooks must be connected by a rope that runs through the hooks in every block on the path between the connected ones. 
When the rope is fastened, a secret door opens. The problem is that we do not know which hooks to connect. That means also that 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 on the first line of the input file. Each test case begins with a line containing two integers C and R (3 <= C,R <= 1000) indicating the number of columns and rows. Then exactly R lines follow, each 
containing C characters. These characters specify the labyrinth. Each of them is either a hash mark (#) or a period (.). Hash marks 
represent rocks, periods are free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks are blocks sharing a common side. We cannot walk diagonally and we cannot step out of the labyrinth. The labyrinth is designed in such a way that
there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is 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 two free blocks, measured in blocks.
Sample Input
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######

Sample Output

Maximum rope length is 0.

Maximum rope length is 8.

Hint

Huge input, scanf is recommended. 

If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc



題意:求迷宮中任意倆'.'間的最大距離。

思路:用兩邊BFS求數的直徑(我們可以任意找一個點(sx,sy),然後找出距離這個點最遠的點(ex,ey),最後從(ex,ey)出發找與其最遠的距離就是樹的直徑,即倆'.'間的最大距離。


AC代碼:

#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
#define H 1005
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int t;//測試數據個數
int R;//行
int C;//列 
int sx, sy, ex, ey, sum;
char map[H][H];//地圖
int vis[H][H];//標記節點是否檢查過
struct node
{
	int x, y, step;
};
bool judge(int a, int b)//判斷下標是否越界
{
	if(a < 0 || a >= R || b < 0 || b >= C)
	    return false;
	else 
	    return true;
} 
void bfs(int x, int y)
{
    memset(vis, 0, sizeof(vis));//把地圖上的所有點都記錄爲還未來過,因爲我們走兩遍BFS,所以記錄在BFS裏面
    queue<node> q;//定義隊列
    vis[x][y] = 1;//標記出發點,證明出發點已經來過了
    node now, next;
    now.x = x;
    now.y = y;
    now.step = 0;
    sum = 0;
    q.push(now);
	while(!q.empty())
	{
		now = q.front();
		q.pop();
		for(int i = 0; i < 4; i++)
		{
			next.x = now.x + dx[i];
			next.y = now.y + dy[i];
			if(judge(next.x, next.y) && map[next.x][next.y] != '#')//判斷條件一個也不能少
			{
				next.step = now.step + 1;
				if(!vis[next.x][next.y])
			        {
			    	        if(sum < next.step)
			    	        {
			    		        sum = next.step;
			    		        ex = next.x;
			    		        ey = next.y;
				        }
				        vis[next.x][next.y] = 1;
				        q.push(next); 
				}
			}
		}
	} 
} 
int main()
{
	scanf("%d", &t);
	while( t-- )
	{
		scanf("%d %d", &C, &R);
		for(int i = 0; i < R; i++)//地圖輸入 
		    scanf("%s", &map[i]);		
		for(int i = 0; i < R; i++)
		for(int j = 0; j < C; j++)
		{	
			if(map[i][j] == '.')
			{
				sx = i;
				sy = j;                           
				break;//題中應該是所有的'.'是聯通的,所以只用找到一個就行了
                                break;
                        }
		bfs(sx, sy);
		bfs(ex, ey);
	    printf("Maximum rope length is %d.\n", sum);    
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章