Labyrinth

题目链接:

http://acm.hust.edu.cn/vjudge/contest/130510#problem/E

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

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.
```

Source


2016-HUST-线下组队赛-5


##题意:

以邻接矩阵的形式给出树,求树的直径.


##题解:

两遍搜索求树的直径即可.
由于数据规模较大,这题很容易爆栈,所以得用bfs.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 1010
#define inf 0x3f3f3f3f
#define mod 1000000007
#define mid(a,b) ((a+b)>>1)
#define IN freopen(".in","r",stdin);
using namespace std;

int n,m;

char mp[maxn][maxn];

bool is_ok(int x, int y) {

return x>=0 && y>=0 && x<n && y<m;

}

int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int ans,px,py;

bool vis[maxn][maxn];

//void dfs(int x, int y, int step) {

// vis[x][y] = 1;

// bool flag = 0;

// for(int i=0; i<4; i++) {

// int xx = x + dir[i][0];

// int yy = y + dir[i][1];

// if(!is_ok(xx,yy) || mp[xx][yy]=='#' || vis[xx][yy]) continue;

// dfs(xx, yy, step+1);

// flag = 1;

// }

//

// if(!flag && step > ans) {

// ans = step;

// px = x, py = y;

// }

//}

struct node {

int x,y,step;

};

void bfs() {

queue q; while(!q.empty()) q.pop();

node cur,next;

memset(vis, 0, sizeof(vis));

cur = {px,py,0}; vis[px][py] = 1;

q.push(cur);

while(!q.empty()) {

bool flag = 0;

cur = q.front(); q.pop();

for(int i=0; i<4; i++) {

int xx = cur.x + dir[i][0];

int yy = cur.y + dir[i][1];

if(!is_ok(xx,yy) || mp[xx][yy]=='#' ||vis[xx][yy]) continue;

vis[xx][yy] = 1;

next = {xx,yy,cur.step+1};

q.push(next);

flag = 1;

}

if(!flag && cur.step > ans) {

ans = cur.step;

px = cur.x; py = cur.y;

}

}

}

int main()

{

IN;

int T;
cin >> T; while (T--){ scanf("%d %d", &m, &n); getchar();
for (int i = 0; i < n; i++) {
gets(mp[i]);
} px = py = -1;
for (int i = 0; i < n; i++) {
for(int j=0; j < m; j++) {
if(mp[i][j] == '.') {
px = i, py = j;
break;
}
}
if(px != -1) break;
} ans = 0;
bfs();
ans = 0;
bfs(); printf("Maximum rope length is %d.\n", ans);
} return 0;

}

最新文章

  1. 进击的Python【第六章】:Python的高级应用(三)面向对象编程
  2. Linux记录从此开始
  3. python第二天-linux权限管理
  4. STL——遍历 删除 set 元素
  5. ecmall模板语法
  6. careercup-链表 2.2
  7. 键盘code码速查表
  8. 项目上传svn出问题
  9. android.intent.action.MAIN与android.intent.category.LAUNCHER
  10. 201521123108《Java程序设计》第12周学习总结
  11. JavaScrip:Function函数编程
  12. webpack的安装与使用
  13. kubernetes系列09—Ingress控制器详解
  14. Spring MVC 使用介绍(十三)数据验证 (一)基本介绍
  15. Linux sed command
  16. ldap配置系列二:jenkins集成ldap
  17. Django+MongoDB批量插入数据
  18. 15. pk-mext
  19. HTTP协议响应码及get请求和post请求比较
  20. Java没有头文件的原因

热门文章

  1. SpringBoot项目启动时执行初始化操作
  2. Linux环境安装mongodb
  3. P1313计算系数
  4. 【vuejs面试题】务必熟知的vuejs面试题「务必收藏」
  5. SCUT - 482 - 生成树上的点 - Prufer
  6. 简单的物流项目实战,WPF的MVVM设计模式(四)
  7. ECMAScript(ES) 6 之 let 与 const
  8. Vue准备
  9. 用element-ui进行文件的上传
  10. babel 转换箭头函数