SPOJ:Labyrinth(最大直线)
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.
Example
Sample Input:
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
####### Sample output:
Maximum rope length is 0.
Maximum rope length is 8.
题意:给定一个N*M的地图,现在要找出地图上面最长的一笔画‘.’的长度。
思路:即是找出最长的连通块的直径。对于每一个块,两次DFS即可。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int dis[maxn][maxn],vis[maxn][maxn];
int cnt,ans,Sx,Sy,Tx,Ty,N,M,times;
int xx[]={,,,-};
int yy[]={,-,,};
char c[maxn][maxn];
void dfs(int x,int y){
vis[x][y]=times;
if(dis[x][y]>ans) ans=dis[x][y];
for(int i=;i<;i++){
if(x+xx[i]>=&&x+xx[i]<=N&&y+yy[i]>=&&y+yy[i]<=M){
if(vis[x+xx[i]][y+yy[i]]!=times&&c[x+xx[i]][y+yy[i]]=='.'){
dis[x+xx[i]][y+yy[i]]=dis[x][y]+;
dfs(x+xx[i],y+yy[i]);
}
}
}
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--){
scanf("%d%d",&M,&N);
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
Sx=Sy=Tx=Ty=ans=;
for(i=;i<=N;i++) scanf("%s",c[i]+);
for(i=;i<=N;i++)
for(j=;j<=M;j++)
if(c[i][j]=='.'&&!vis[i][j]){
Sx=Tx=i; Sy=Ty=j; times++;
dis[Sx][Sy]=; dfs(Sx,Sy); //第一次dfs
for(i=;i<=N;i++)
for(j=;j<=M;j++)
if(dis[i][j]>dis[Tx][Ty]) Tx=i,Ty=j;
dis[Tx][Ty]=; times++;
dfs(Tx,Ty); //第二次dfs
}
printf("Maximum rope length is %d.\n",ans);
}
return ;
}
SPOJ:Labyrinth(最大直线)的更多相关文章
- UVALive 4639 && SPOJ SPOINTS && POJ 3805 && AOJ 1298 Separate Points 求两个凸包是否相交 难度:3
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- MFC中如何画带实心箭头的直线
工作中遇到话流程图的项目,需要画带箭头的直线,经过摸索,解决:思路如下: (1) 两个点(p1,p2)确定一个直线,以直线的一个端点(假设p2)为原点,设定一个角度 (2)以P2为原点得到向量P2P1 ...
- 水平可见直线 bzoj 1007
水平可见直线 (1s 128M) lines [问题描述] 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆 ...
- SVG:linearGradient渐变在直线上失效的问题解决方案
SVG开发里有个较为少见的问题. 对x1=x2或者y1=y2的直线(line以及path),比如: <path d="M200,10 200,100" stroke=&quo ...
- 封装 用canvas绘制直线的函数--面向对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- [bzoj1007][HNOI2008][水平可见直线] (斜率不等式)
Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为 可见的,否则Li为被覆盖的. 例如,对于直线: L1:y ...
- [LeetCode] Line Reflection 直线对称
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
随机推荐
- Jquery跨域请求
在JavaScript中,有一个很重要的安全性限制,被称为“Same- Origin Policy”(同源策略).这一策略对于JavaScript代码能够访问的页面内容做了很重要的限制,即JavaSc ...
- oracle导出多CSV文件的靠谱的
oracle导出多CSV文件的问题 ---------------------------------------------------------------------- 用ksh脚本从orac ...
- MyBatis使用Mapper动态代理开发Dao层
开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...
- HDU1272 迷宫通路数
Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该 ...
- [luoguP1773] 符文之语_NOI导刊2010提高(02)(DP)
传送门 f[i][j]表示前i个数余数为j的最优解 sum[i][j]表示字符串i~j所构成的数 #include <cstdio> #include <cstring> #d ...
- msp430入门编程12
msp430中C语言的模块化头文件及库文件12 msp430入门学习 msp430入门编程
- 使用Navicat进行数据库对比同步
使用Navicat进行数据库对比同步 当有多个数据库时,有时会出现结构或者数据不同步的问题,这时候可以使用navivat工具对比同步( 我的Navicat版本是11.0.17). 参考博客: 岁伏的博 ...
- 代码svn下载到本地后,关于数据库问题
代码svn下载到本地后,关于数据库问题 1.那我本地还用搭建相应的数据库么?答案:当然不用啦,本地系统里已经配置好了数据库的网络地址了,端口号,密码啥的.即使你代码运行在本地,依然可以将数据传输到服务 ...
- Meteor部
一个关于 Meteor 主要事项就是如何轻松部署应用程序.当程序完成后,有一个简单的方法来和世界分享你的应用程序.所有需要做的就是在运行命令提示符窗口下面的代码. C:\Users\Administr ...
- 【Scrapy】Selectors
Constructing selectors For convenience,response objects exposes a selector on .selector attribute,it ...