1145. Rope in the Labyrinth

Time limit: 0.5 second
Memory limit: 64 MB
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 n characters. 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 output
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
8
 
Difficulty: 425
 
题意:.是可以走的,#不可以走。所有的.组成了一棵树,问这棵树的直径。
分析:求树的直径。bfs即可。
 
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
const int DX[] = {-, , , }, DY[] = {, -, , };
int n, m;
char graph[N][N];
int dp[N][N];
queue<pair<int, int> > que; inline void Input()
{
scanf("%d%d", &m, &n);
for(int i = ; i < n; i++) scanf("%s", graph[i]);
} inline bool Check(int x, int y)
{
if(x < || y < || x >= n || y >= m) return ;
if(graph[x][y] != '.') return ;
return ;
} inline void Bfs(int sx, int sy)
{
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
dp[i][j] = INF;
que.push(mk(sx, sy));
dp[sx][sy] = ;
while(sz(que))
{
int ux = que.front().ft, uy = que.front().sd;
que.pop();
for(int t = ; t < ; t++)
{
int vx = ux + DX[t], vy = uy + DY[t];
if(Check(vx, vy) && dp[vx][vy] > dp[ux][uy] + )
{
dp[vx][vy] = dp[ux][uy] + ;
que.push(mk(vx, vy));
}
}
}
} inline void GetMax(int &px, int &py)
{
int mx = -INF;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(mx < dp[i][j] && dp[i][j] < INF)
{
mx = dp[i][j];
px = i, py = j;
}
} inline void Solve()
{
bool flag = ;
for(int i = ; i < n && !flag; i++)
for(int j = ; j < m && !flag; j++)
if(graph[i][j] == '.')
{
Bfs(i, j);
flag = ;
} int px, py;
GetMax(px, py);
Bfs(px, py); GetMax(px, py);
printf("%d\n", dp[px][py]);
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

ural 1145. Rope in the Labyrinth的更多相关文章

  1. URAL 1145—— Rope in the Labyrinth——————【求树的直径】

    Rope in the Labyrinth Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  2. ural 1020 Rope

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  3. URAL.1033 Labyrinth (DFS)

    URAL.1033 Labyrinth (DFS) 题意分析 WA了好几发,其实是个简单地DFS.意外发现这个俄国OJ,然后发现ACRUSH把这个OJ刷穿了. 代码总览 #include <io ...

  4. URAL 1033 Labyrinth

    E - Labyrinth Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...

  5. [POJ1383]Labyrinth

    [POJ1383]Labyrinth 试题描述 The northern part of the Pyramid contains a very large and complicated labyr ...

  6. ural 1246. Tethered Dog

    1246. Tethered Dog Time limit: 1.0 secondMemory limit: 64 MB A dog is tethered to a pole with a rope ...

  7. ural 1152. False Mirrors

    1152. False Mirrors Time limit: 2.0 secondMemory limit: 64 MB Background We wandered in the labyrint ...

  8. poj 1383 Labyrinth

    题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...

  9. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

随机推荐

  1. @ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4 "/" $NF}'中"[ :]+" 是什么意思?@

    http://blog.csdn.net/zhuying_linux/article/details/6822987

  2. GBDT原理实例演示 2

        一开始我们设定F(x)也就是每个样本的预测值是0(也可以做一定的随机化) Scores = { 0, 0, 0, 0, 0, 0, 0, 0}     那么我们先计算当前情况下的梯度值     ...

  3. 【C#】 用Route进行URL重写

    在.NET Framework 4中,微软推出了Route机制.这种机制不仅在MVC中大量运用,在WebForm中也可以使用. 和Contex.RewritePath()一样,Route功能也是写在G ...

  4. 讲解JS的promise,这篇是专业认真的!

    http://www.zhangxinxu.com/wordpress/2014/02/es6-javascript-promise-%E6%84%9F%E6%80%A7%E8%AE%A4%E7%9F ...

  5. 攻城狮在路上(肆)How tomcat works(零) 前言说明

    最近几篇是关于How tomcat works一书的读书笔记. 通过数个章节逐渐实现一个tomcat的功能. 源码下载地址:http://zhidao.baidu.com/share/7007af0f ...

  6. 在WPF中使用CefSharp嵌入浏览器

    日常开发中,我们需要将一些Web页面嵌入到桌面客户端软件中.下面我们使用CefSharp嵌入浏览器来实现. 首先先介绍一下CefSharp嵌入式浏览器,它是基于Google浏览器的一个组件,我们可以在 ...

  7. Java后端WebSocket的Tomcat实现

    转自:http://blog.chenzuhuang.com/archive/28.html 文章摘要随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5 ...

  8. .deb文件打包

    最近因项目需要,需要把文件夹打包为.deb格式的包,幸亏一位朋友帮忙指导了我一个晚上,才得以完成,这里再次对他表示感谢. 整理打包流程如下: 请先参考此博客内容,了解deb文件打包 如何制作Deb包和 ...

  9. ICP 算法步骤

    The Iterative Closest Point (ICP) is an algorithm employed to match two surface representations, suc ...

  10. GitHub上史上最全的Android开源项目分类汇总 (转)

    GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...