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 ofthem 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, eachcontaining 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
- POJ 1383 Labyrinth(BFS 树的直径)
- Poj 1383--Labyrinth【树的直径】【bfs】
- poj 1383 Labyrinth 树的直径
- POJ 1383--Labyrinth【树的直径】
- poj--1383--Labyrinth(树的直径)
- 【POJ】1383 - Labyrinth(树的直径)
- POJ Problem 1383 Labyrinth【树的直径】
- POJ 1383--Labyrinth【树的直径】
- POJ 1383 Labyrinth (树的直径)
- poj 1383 Labyrinth(树的直径)
- poj-1383-Labyrinth【树的直径】【经典】
- POJ 1383 Labyrinth(树的直径)
- poj 1383 Labyrinth 【迷宫图 BFS实现 树的直径裸题】
- POJ1383 Labyrinth(树的直径:两次BFS)
- POJ 1383 Labyrinth 【树的直径】【真正的图】
- poj 1383 Labyrinth(树的直径详解,简单明了!)
- 【POj】-1383-Labyrinth(树的直径,地图)
- poj 1383 Labyrinth bfs
- PHP获取重定向URL的几种方法
- 一牛网-android
- 正则表达式
- JavaEE平台下CRM系统开发
- 发布流程演进之路
- POJ 1383 Labyrinth(BFS 树的直径)
- 51nod 最长单增子序列(LIS)
- arduino引脚
- RHEL5.4 x86_64 GCC 4.1.2 升级到4.7.0版过程(含安装MPFR、GMP、MPC过程)
- 剑指Offer_27_字符串的排列
- IOS 之 PJSIP 笔记(一) 编译多平台支持的静态库
- Oracle之range,hash,list分区现实应用及优缺点汇总
- select,poll,epoll的比较
- 2016多校集训---hdu5852