ural 1145. Rope in the Labyrinth
1145. Rope in the Labyrinth
Time limit: 0.5 second
Memory limit: 64 MB
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 ≤ n, m ≤ 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
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 }