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. Java网络连接之HttpURLConnection 与 HttpClient

    HttpClient使用详解:http://blog.csdn.net/wangpeng047/article/details/19624529   注:HttpURLConnection输出流用ou ...

  2. CLR via C#(06)- 构造器

    最近忙着看新还珠,好几天不学习了.玩物丧志啊,罪过罪过. 今天总结的是类构造器的知识,其实这方面的文章蛮多的,可还是觉得亲自写一下对自己的思考和认识会有提高. 对于构造器,大家应该都不陌生,它主要是用 ...

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

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

  4. 安装oracle 12c RAC遇到的一些问题

    (1) 安装grid软件,停止在38%很长时间不动,日志显示正常   解决方法: 由于是虚拟机安装,设置的内存为600M,关闭虚拟机,把内存调成1GB,问题解决~在38%Linking RMAN Ut ...

  5. 攻城狮在路上(叁)Linux(十五)--- 文件与目录的默认权限与隐藏权限

    一.文件默认权限:umask <==需要被减去的权限. 1.umask指的是当前用户在新建文件或者目录时的默认权限,如0022; 2.默认情况下,用户创建文件的最大权限为666; 创建目录的最大 ...

  6. document.location.reload();与location.href='xxx'的区别

    document.location.reload();会重新加载页面,onload事件会被触发. location.href='xxx'刷新页面,onload事件不会触发.

  7. Java Hour 62 J2EE App 服务器

    目前略微瓶颈了,准备换工作. tomcat.weblogic.jboss的区别,容器的作用 Apache 是一个http 服务器. Tomcat 是一web 应用程序服务器,支持部分的j2ee. Jb ...

  8. WPF QuickStart系列之线程模型(Thread Model)

    这篇博客将介绍WPF中的线程模型. 首先我们先来看一个例子,用来计算一定范围内的素数个数. XAML: <Grid> <Grid.RowDefinitions> <Row ...

  9. uC/OS II原理分析及源码阅读(一)

    uC/OS II(Micro Control Operation System Two)是一个可以基于ROM运行的.可裁减的.抢占式.实时多任务内核,具有高度可移植性,特别适合于微处理器和控制器,是和 ...

  10. 配置ogg异构oracle-mysql(1)基础环境配置

    一.环境描述: 192.168.0.164 ( Oracle ) —> 192.168.0.165 (Mysql ) 版本: 操作系统:redhat5.8 Oracle:  11.2.0.3 M ...