Labyrinth POJ - 1383

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
 
 
题意:由 ' . '构成的最长路
题解:树的直径
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e3+;
const int INF=0x3f3f3f3f;
int n,m,ans;
char map[maxn][maxn];
int dis[maxn][maxn];
int dx[] = {,-,,};
int dy[] = {,,,-};
struct node{
int x,y;
};
node second;
void bfs(node first)
{
ans = ;
memset(dis,-,sizeof dis);
queue<node> que;
node p = {first.x,first.y};
dis[first.x][first.y] = ;
que.push(p);
while(!que.empty())
{
node tmp = que.front();
que.pop();
for(int i=; i<; i++)
{
int nx = tmp.x + dx[i];
int ny = tmp.y + dy[i];
if(nx>= && nx < m && ny>= && ny<n && map[nx][ny] != '#' && dis[nx][ny] == -)
{
node Next;
Next.x = nx;
Next.y = ny;
que.push(Next);
dis[nx][ny] = dis[tmp.x][tmp.y] + ;
if(ans < dis[nx][ny])
{
ans = dis[nx][ny];
second.x = nx;
second.y = ny;
}
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
struct node first;
scanf("%d %d",&n,&m);
getchar();
for(int i=; i<m; i++)
scanf("%s",map[i]);
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(map[i][j] == '.')
{
first.x = i;
first.y = j;
}
}
bfs(first);
bfs(second);
printf("Maximum rope length is %d.\n",ans);
}
}

最新文章

  1. div不换行_div同行_div强制不换行
  2. C/C++ 结构体 函数传递
  3. c#访问Oracle问题及解决方法
  4. [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
  5. 丁又专老师作业——Java检测代码
  6. tyvj1161聚会的名单(trie树)
  7. OKHttp的简单使用
  8. 百度云观测优化建议解决方案:未设置max-age或expires
  9. Android 标签控件
  10. AJAX及其跨域的主要解决方法
  11. HP quality center 9.0 邮件设置
  12. Openjudge-NOI题库-出书最多
  13. SSM(Maven集成)
  14. iOS 模拟器安装应用
  15. 【转载】Centos7 中使用Supervisor守护进程
  16. I/O-----字符输入流
  17. app每次更新版本时调用js代码提示用户下载更新
  18. IT技术
  19. [Python]_ELVE_centos7安装Python3.7.1与Python2共存
  20. NDK toolchain对应ABI

热门文章

  1. Java排序算法(一)
  2. 解决ARCGIS10.2与VS2013不兼容
  3. 执行命令npm install XXX后仍然提示 Cannot find Module XXX
  4. mui的ajax例子1
  5. 编译安装PHP-7.1.8
  6. Jsoup进阶选择器
  7. StackOverflow之旅&lt;1&gt;------{去掉烦人的&quot;!=null&quot;判断}
  8. IOS 九宫图解锁(封装)
  9. 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法
  10. python模块;opencv安装