Rope in the Labyrinth

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are parallel with the labyrinth's sides. Each cell of the grid is either occupied or free. It is possible to move from one free cell to another free cells that share a common side with the cell. One cannot move beyond the labyrinth's borders. The labyrinth is designed pretty specially: for any two cells there is only one way to move from one cell to the other. There is a hook at each cell's center. In the labyrinth there are two special free cells, such that if you can connect the hooks of those two cells with a rope, the labyrinth's secret door will be automatically opened. The problem is to prepare a shortest rope that can guarantee, you always can connect the hooks of those two cells with the prepared rope regardless their position in the labyrinth.

Input

The first line contains integers n and m (3 ≤ nm ≤ 820). The next lines describe the labyrinth. Each of the next m lines contains ncharacters. Each character is either "#" or ".", with "#" indicating an occupied cell, and "." indicating a free cell.

Output

Print out in the single line the length (measured in the number of cells) of the required rope.

Sample Input

input output
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
8

题目大意:给你一个图,"."表示你可以走,"#“表示墙不能走,每个格子都有一个钩。任意两个格子之间只有一条路,现在问你最短能让所有"."的格子中的钩能用绳子连接的绳子长度。

解题思路:其实就是让你求树的直径的。由"."构成的是一棵树,然后求树的直径,两次广搜即可。

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
typedef long long LL;
const int maxn = 900;
char Map[maxn][maxn];
bool vis[maxn][maxn];
int f[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
struct Node{
int x,y,step;
};
queue<Node>Q;
bool jud(int i,int j){
Node st;
int ret = 0;
if(Map[i-1][j] == '#'){
ret++;
}
if(Map[i][j-1] == '#'){
ret++;
}
if(Map[i+1][j] == '#'){
ret++;
}
if(Map[i][j+1] == '#'){
ret++;
}
if(ret >= 3){
st.x = i, st.y = j, st.step = 0;
Q.push(st);
return true;
}
return false;
}
int n,m;
Node BFS(){
Node st,tmp,en;
en = Q.front();
vis[en.x][en.y] = 1;
while(!Q.empty()){
st = Q.front();
Q.pop();
if(st.step > en.step){
en = st;
}
int tmpx ,tmpy;
for(int i = 0; i < 4; i++){
tmp.x = st.x + f[i][0];
tmp.y = st.y + f[i][1];
if(tmp.x <= 0 ||tmp.x > m ||tmp.y <= 0 || tmp.y > n || Map[tmp.x][tmp.y] =='#' ||vis[tmp.x][tmp.y]){
continue;
}
vis[tmp.x][tmp.y] = 1;
tmp.step = st.step + 1;
Q.push(tmp);
}
}
return en;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
while(!Q.empty()) Q.pop();
for(int i = 1; i <= m; i++){
getchar();
for(int j = 1; j <= n; j++){
scanf("%c",&Map[i][j]);
}
}
int flag = 0;
for(int i = 1; i <= m; i++){
if(flag) break;
for(int j = 1; j <= n; j++){
if(Map[i][j] == '.' && flag == 0){
flag = jud(i,j);
}
if(flag) break;
}
}
Node st = BFS();
memset(vis,0,sizeof(vis));
// printf("%d %d %d+++\n",st.x,st.y,st.step);
st.step = 0;
Q.push(st);
Node en = BFS();
printf("%d\n",en.step);
}
return 0;
}

  

URAL 1145—— Rope in the Labyrinth——————【求树的直径】的更多相关文章

  1. ural 1145. Rope in the Labyrinth

    1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...

  2. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  3. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  4. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  5. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  6. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

  7. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

  8. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  9. 求树的直径+并查集(bfs,dfs都可以)hdu4514

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...

随机推荐

  1. 关于webapi练习过程中遇到的一系列问题记录

    最近在尝试本地进行webapi调用的过程中,遇到一系列的问题,demo很小但着实让人头疼,先附上demo. 前台页面,目的是展示新闻的分类: 类别模型如下: 控制器代码如下: public Actio ...

  2. [CentOS7] 安装sogou输入法

    CentOS7 下的默认输入法不是很好用,于是还是用sogou输入法 由于官网只有Ubuntu版本的sogou输入法安装包,于是先下载下来再说,博主用的版本在这里(密:ph13): 接下来解压data ...

  3. 写一个Android输入法02——候选窗、转换

    上一篇介绍了完成Android输入法的最小化步骤,它只能将按键对应的字符上屏.一般的东亚语言都有一个转换的过程,比如汉语输入拼音,需要由拼音转成汉字再上屏.本文将在前文基础上加入完成转换过程所必需的候 ...

  4. Jquery学习理解 (课堂)

    Jquary概述 3 1.Jquary简介 3 1.1什么是jquery 3 2.使用jquary 4 2.1浏览器如何解析HTML文件 4 2.2利用选择器定位节点 4 2.3调用方法操作节点 5 ...

  5. loj#6363. 「地底蔷薇」(拉格朗日反演+多项式全家桶)

    题面 传送门 题解 肝了一个下午--我老是忘了拉格朗日反演计算的时候多项式要除以一个\(x\)--结果看它推倒简直一脸懵逼-- 做这题首先你得知道拉格朗日反演是个什么东西->这里 请坐稳,接下来 ...

  6. mysql远程访问被禁止

    远程连接Mysql服务器的数据库,错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx  is not allowed to connect to this MySQL ...

  7. Chrome插件-Postman Interceptor

    postman有一个chrome插件 Postman Interceptor,可以让postman中发送请求的时候使用这个网站的浏览器cookie Postman Interceptor,可以让pos ...

  8. Jmeter实现从csv文件中随机读取数据

    一.需求 参数放在csv文件中,文件格式如下,需求每次从文件中随机读取一行数据. 二.步骤 1.在csv文件中新增加一列,pl 2.新增一个配置原件-随机数,设置如下: 50是文件数据的行数 3.新增 ...

  9. java方法里面生成js弹出框

    核心代码:方法参数要有response response.setContextType("text/html;charset=UTF-8"); PrintWrite out = r ...

  10. HTTP,HTTPS端口号

    博主暂做个笔记,查询资料再纠正~~~~小白不要不假思索的抄走~~~ 一般情况下,HTTP默认工作端口是8000,HTTPS默认工作端口是443