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. DB2 SQL Mixed data in character strings

    Mixed character data and graphic data are always allowed for Unicode, but for EBCDIC and ASCII, the ...

  2. 3.2 STL中的函数对象类模板

    *: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...

  3. PostgreSQL中COUNT的各条件下(1亿条数据)例子

    test=# insert into tbl_time1 select generate_series(1,100000000),clock_timestamp(),now(); INSERT 0 1 ...

  4. 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)

    前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...

  5. C#学习笔记---协变和逆变

    http://www.cnblogs.com/alphafly/p/4048608.html 协变是指方法能从委托的返回类型派生的一个类型. 逆变之方法获取的参数可以是委托参数类型的基类.

  6. ***LINUX添加PHP环境变量:CentOS下将php和mysql命令加入到环境变量中

    CentOS系统下如何将PHP和mysql命令加入到环境变量中,在Linux CentOS系统上 安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到 ...

  7. IIS7 经典模式和集成模式的区别(转载)

    转载地址:http://www.poluoluo.com/server/201301/193110.html 升级过程中出现了比较多的问题,前面文章也提到过几个.这次就主要介绍下httpHandler ...

  8. 谈谈Delphi中的类和对象4---类是一种对数据和操作高度的封装机制 && 类是一种代码重用机制

    五.类是一种对数据和操作高度的封装机制 1)数据封装 unit Unit2; interface type TEmployee = class; private FName: String; publ ...

  9. Pyqt 屏幕截图工具

    从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py) 所以想自己UI下界面,手动练习下 通过UI生成的: Screenshot.py ...

  10. MVC4 使用概述

    1.Bundle使用: http://www.cnblogs.com/inline/p/3897256.html 2.MVC总结: http://www.cnblogs.com/xlhblogs/ar ...