ural 1145. Rope in the Labyrinth

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

Tags: graph theory  (hide tags for unsolved problems)

Difficulty: 425

题意:.是可以走的,#不可以走。所有的.组成了一棵树,问这棵树的直径。

分析:求树的直径。bfs即可。

  1 /**
  2 Create By yzx - stupidboy
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <deque>
  9 #include <vector>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <map>
 14 #include <set>
 15 #include <ctime>
 16 #include <iomanip>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef double DB;
 20 #define MIT (2147483647)
 21 #define INF (1000000001)
 22 #define MLL (1000000000000000001LL)
 23 #define sz(x) ((int) (x).size())
 24 #define clr(x, y) memset(x, y, sizeof(x))
 25 #define puf push_front
 26 #define pub push_back
 27 #define pof pop_front
 28 #define pob pop_back
 29 #define ft first
 30 #define sd second
 31 #define mk make_pair
 32
 33 inline int Getint()
 34 {
 35     int Ret = 0;
 36     char Ch = ‘ ‘;
 37     bool Flag = 0;
 38     while(!(Ch >= ‘0‘ && Ch <= ‘9‘))
 39     {
 40         if(Ch == ‘-‘) Flag ^= 1;
 41         Ch = getchar();
 42     }
 43     while(Ch >= ‘0‘ && Ch <= ‘9‘)
 44     {
 45         Ret = Ret * 10 + Ch - ‘0‘;
 46         Ch = getchar();
 47     }
 48     return Flag ? -Ret : Ret;
 49 }
 50
 51 const int N = 1010;
 52 const int DX[] = {-1, 0, 1, 0}, DY[] = {0, -1, 0, 1};
 53 int n, m;
 54 char graph[N][N];
 55 int dp[N][N];
 56 queue<pair<int, int> > que;
 57
 58 inline void Input()
 59 {
 60     scanf("%d%d", &m, &n);
 61     for(int i = 0; i < n; i++) scanf("%s", graph[i]);
 62 }
 63
 64 inline bool Check(int x, int y)
 65 {
 66     if(x < 0 || y < 0 || x >= n || y >= m) return 0;
 67     if(graph[x][y] != ‘.‘) return 0;
 68     return 1;
 69 }
 70
 71 inline void Bfs(int sx, int sy)
 72 {
 73     for(int i = 0; i < n; i++)
 74         for(int j = 0; j < m; j++)
 75             dp[i][j] = INF;
 76     que.push(mk(sx, sy));
 77     dp[sx][sy] = 0;
 78     while(sz(que))
 79     {
 80         int ux = que.front().ft, uy = que.front().sd;
 81         que.pop();
 82         for(int t = 0; t < 4; t++)
 83         {
 84             int vx = ux + DX[t], vy = uy + DY[t];
 85             if(Check(vx, vy) && dp[vx][vy] > dp[ux][uy] + 1)
 86             {
 87                 dp[vx][vy] = dp[ux][uy] + 1;
 88                 que.push(mk(vx, vy));
 89             }
 90         }
 91     }
 92 }
 93
 94 inline void GetMax(int &px, int &py)
 95 {
 96     int mx = -INF;
 97     for(int i = 0; i < n; i++)
 98         for(int j = 0; j < m; j++)
 99             if(mx < dp[i][j] && dp[i][j] < INF)
100             {
101                 mx = dp[i][j];
102                 px = i, py = j;
103             }
104 }
105
106 inline void Solve()
107 {
108     bool flag = 0;
109     for(int i = 0; i < n && !flag; i++)
110         for(int j = 0; j < m && !flag; j++)
111             if(graph[i][j] == ‘.‘)
112             {
113                 Bfs(i, j);
114                 flag = 1;
115             }
116
117     int px, py;
118     GetMax(px, py);
119     Bfs(px, py);
120
121     GetMax(px, py);
122     printf("%d\n", dp[px][py]);
123 }
124
125 int main()
126 {
127     freopen("a.in", "r", stdin);
128     Input();
129     Solve();
130     return 0;
131 }

时间: 2024-11-06 19:13:30

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

URAL 1145. Rope in the Labyrinth(两次BFS啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1145 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 paralle

ural 1145 Rope in the Labyrinth 图中 bfs求树的直径

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

ural 1020. Rope(几何)

题目链接:ural 1020. Rope 题目大意:按照顺序给定N个点,每个点有半径R,问说用线环绕N个点所需要的长度. 解题思路:因为需要围成一个圈,所以旋转角度一定是一周,板径又都相同,所以直接就是两两点之间的距离加上一个周长. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 1

URAL 1020 Rope

Java的类库还是能省点力气的…… 1 import java.awt.geom.Point2D; 2 import java.util.Scanner; 3 4 public class P1020 5 { 6 public static void main(String args[]) 7 { 8 try (Scanner cin = new Scanner(System.in)) 9 { 10 while (cin.hasNext()) 11 { 12 int n = cin.nextIn

SYSU暑假热身赛D

D - Rope in the Labyrinth Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1145 Description A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by l

URAL 1033 Labyrinth

E - Labyrinth Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1033 Description Administration of the labyrinth has decided to start a new season with new wallpapers. For this purpose they need

URAL 1033 Labyrinth(DFS)

Administration of the labyrinth has decided to start a new season with new wallpapers. For this purpose they need a program to calculate the surface area of the walls inside the labyrinth. This job is just for you! The labyrinth is represented by a m

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. The pole is located inside a fenced polygon (not necessarily convex) with nonzero area. The fence has no self-crosses. The Olympian runs along the f

POJ 1383 Labyrinth

Labyrinth Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on PKU. Original ID: 138364-bit integer IO format: %lld      Java class name: Main The northern part of the Pyramid contains a very large and complicated labyrinth. The la