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. eclipse maven tomcat7 热部署

    .配置tomcat a.配置jdk b.CATALINA_HOME=c:\tomcat CATALINA_BASE=c:\tomcat .tomcat配置密码 C:\Program Files\oth ...

  2. 数据结构和算法 – 3.堆栈和队列

    1.栈的实现   后进先出     自己实现栈的代码 using System; using System.Collections.Generic; using System.Linq; using ...

  3. .net学习之委托和事件

    1.什么是委托通俗的说:委托就是一个能够存储符合某种格式(方法签名)的方法的指针的容器上传图片: 2.委托语法准备一个方法:string Hello(string userName){} string ...

  4. EChart使用简单介绍

    Echart是百度研发团队开发的一款报表视图JS插件,功能十分强大,使用内容做简单记录:(EChart下载地址 http://echarts.baidu.com/download.html) 1.ti ...

  5. 【JAVA集合框架之List】

    一.List接口概述. List有个很大的特点就是可以操作角标. 下面开始介绍List接口中相对于Collection接口比较特别的方法.在Collection接口中已经介绍的方法此处就不再赘述. 1 ...

  6. Oracle 数组赋值

    只需要像下面这样就OK了 begin -- Call the procedure in_var(1):=null;in_var(1):='a123123'; pack_abc.pro_abc(in_v ...

  7. C# 将文件转化成byte[]数组

    /// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl"& ...

  8. 6-01T-SQL中的运算符

    算术运算符:+.-.*./.%. 赋值运算符:= 逻辑运算符:AND.OR.NOT. 比较运算符:>,<,<=,>=,<>.=,!=. 连接运算符:"+& ...

  9. C# 与 Microsoft Expression Encoder实现屏幕录制

    在日常开发中,我们会经常遇到屏幕录制的需求.在C#中可以通过Expression Encoder的SDK实现这样的需求.首先需要下载Expression Encoder SDK,实现代码: priva ...

  10. Java关键字native、volatile、transient

    native native是方法修饰符.Native方法是由另外一种语言(如c/c++,FORTRAN,汇编)实现的本地方法.一般用于JNI中. native关键字说明其修饰的方法是一个原生态方法,方 ...