树的直径
来源:互联网 发布:点赞赚钱软件 编辑:程序博客网 时间:2024/06/05 03:05
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
Sample Output
Maximum rope length is 0.
Maximum rope length is 8.
Hint
题意
题解:
树的直径求法 两次bfs 第一次找到s-t中的一个点 第二次从这个点开始找到另一个点
AC代码
#include<cstdio>#include<cstring>#include<stack>#include <set>#include <queue>#include <vector>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int mod = 1e9+7;int n,m,flag;char st[1005][1005];int vis[1005][1005];int to[2][4] = {1,0,-1,0,0,1,0,-1};int sx,sy;int ans;struct node{ int x,y,step;};void bfs(int x,int y){ memset(vis,0,sizeof(vis)); queue<node > q; node a,b; a.x = x; a.y = y; a.step = 0; q.push(a); vis[x][y] = 1; ans = 0; while (!q.empty()){ b = q.front();q.pop(); node s; for (int i = 0;i < 4; ++i){ s.x = b.x + to[0][i]; s.y = b.y + to[1][i]; s.step = b.step + 1; if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&!vis[s.x][s.y]&&st[s.x][s.y]=='.'){ vis[s.x][s.y] = 1; if (s.step > ans){ ans = s.step; sx = s.x; sy = s.y; } q.push(s); } } } }int main(){ int t; scanf("%d",&t); while (t--){ sx = 0,sy = 0; flag = 0; scanf("%d%d",&m,&n); for (int i= 0;i < n; ++i){ scanf("%s",st[i]); if (flag) continue; for (int j = 0;j < m; ++j){ if (st[i][j]=='.'){ flag = 1; sx = i,sy = j; } } } bfs(sx,sy); bfs(sx,sy); printf("Maximum rope length is %d.\n",ans); } return 0;}
- bzoj3124 [Sdoi2013]直径 树的直径
- 【树的直径】
- 树的直径
- 求树的直径
- 树的最长直径
- HDU4607 树的直径
- URAL1056(树的直径)
- 树的直径hdu3721
- 树的直径
- 树的直径
- 树的重心、直径
- 树的直径
- 树的直径 【总结】
- poj1985 树的直径
- 树的直径
- 树的直径
- hdu4607 树的直径
- 树的直径
- 最短路径问题
- StringUtils.join的用法:
- CSS基础语法
- MFC定时器SetTimer
- PCI Geomatica v9.1完整好用\
- 树的直径
- C++字符集和词法记号
- docker打镜像脚本
- 数据存储上
- 使用elasticsearch-head创建新文档
- SpringMVC笔记
- 关于赋值指向的一点知识
- 《leetCode》:add two num
- iOS开发 调用系统的震动和提示音