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. python基础——sorted()函数

    python基础——sorted()函数 排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个d ...

  2. linux /etc/profile和/etc/bashrc

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,并从/etc/profile.d目录的配置文件中搜集shell的设置,/etc/bashrc:为每一个运 ...

  3. Django搭建简易博客

    Django简易博客,主要实现了以下功能 连接数据库 创建超级用户与后台管理 利用django-admin-bootstrap美化界面 template,view与动态URL 多说评论功能 Markd ...

  4. struts2中一些常用的写法 记录

    1.对日期进行处理 Date current = new Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ...

  5. bnuoj 24251 Counting Pair

    一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...

  6. DOM – 7.动态创建DOM + 8.innerText innerHTML value

    7.动态创建DOM 8.innerText  innerHTML  value 7+8 练习:案例:点击按钮动态增加网站列表,分两列,第一列为网站的名字,第二列为带网站超链接的网站名.增加三行常见网站 ...

  7. ASP.NET MVC 使用带有短横线的html Attributes(转载)

    转载地址:http://www.nmtree.net/2013/10/25/asp-net-mvc-use-dash-in-html-attributes.html 情景再现 我们常常需要一个文本框来 ...

  8. 使用 Log4Net 记录日志

    第一步:下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的  log4net-1.2.11-bin-n ...

  9. C# XML操作

    一.简单介绍 using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xml.Load(path); ...

  10. hdu 4731 2013成都赛区网络赛 找规律

    题意:找字串中最长回文串的最小值的串 m=2的时候暴力打表找规律,打表可以用二进制枚举