POJ 1383 Labyrinth(BFS 树的直径)

来源:互联网 发布:煤炭建设工程预算软件 编辑:程序博客网 时间:2024/05/29 04:44

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 1005int 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;}
0 0
原创粉丝点击