URAL 1145. Rope in the Labyrinth(两次BFS啊 )
2015-02-08 21:06
363 查看
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1145
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.
Each character is either "#" or ".", with "#" indicating an occupied cell, and "." indicating a free cell.
PS:
连续两次BFS求最长路径!
代码如下:
1145. Rope in the Labyrinth
Time limit: 0.5 secondMemory 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 |
连续两次BFS求最长路径!
代码如下:
#include <cstdio> #include <cstring> #include <queue> #include <iostream> #include <algorithm> using namespace std; int xx[4] = {0,0,1,-1}; int yy[4] = {1,-1,0,0}; char mp[1047][1047]; int n, m; int vis[1047][1047]; struct node { int x, y; int step; }; int judge(int x, int y) { if(x>=0 && x<n && y>=0 && y<m && mp[x][y]=='.' && !vis[x][y]) return 1; return 0; } node BFS(int x, int y) { memset(vis,0,sizeof(vis)); queue<node> q; while(!q.empty()) { q.pop(); } node rear, front, p; front.x = x, front.y = y; front.step = 0; p.x = x, p.y = y; p.step = 0; q.push(front); vis[x][y] = 1; while(!q.empty()) { front = q.front(); q.pop(); if(front.step > p.step) { p = front; } for(int i = 0; i < 4; i++) { int dx = front.x+xx[i]; int dy = front.y+yy[i]; if(judge(dx,dy)) { vis[dx][dy] = 1; rear.x = dx,rear.y = dy; rear.step = front.step+1; q.push(rear); } } } return p; } int main() { while(~scanf("%d%d",&m,&n)) { getchar(); for(int i = 0; i < n; i++) { gets(mp[i]); } int flag = 0; int x = 0, y = 0; for(int i = 0; i < n; i++)//随机寻找一个'.' { for(int j = 0; j < m; j++) { if(mp[i][j] == '.') { x = i; y = j; flag = 1; break; } } if(flag) break; } node tt = BFS(x, y); node ans = BFS(tt.x, tt.y); printf("%d\n",ans.step); } return 0; }
相关文章推荐
- URAL 1145—— Rope in the Labyrinth——————【求树的直径】
- URAL - 1145 Rope in the Labyrinth
- 1145. Rope in the Labyrinth
- 【树直径】UARL 1145 Rope in the Labyrinth
- Light OJ 1094 - Farthest Nodes in a Tree【树的直径 两次bfs】
- 程序员必看之★编程原则(中英文对照)★--《Enough Rope to Shoot Yourself in the Foot
- URAL 1804 The Machinegunners in a Playoff (比较绕的分类)
- HOJ 1030 Labyrinth----------------两次BFS求树的直径(图的最长路)
- Ural-1084. Goat in the Garden(计算几何)
- URAL 1348 Goat in the Garden 2(点到线段的距离)
- URAL 1084 || Goat in the Garden(同中心矩形与圆的公共面积
- Ural_1348. Goat in the Garden 2(计算几何)
- URAL 1804 The Machinegunners in a Playoff(模拟比赛得分)
- ural 1084. Goat in the Garden
- URAL 1643 Attack of the Dark Fortress BFS
- URAL 1348. Goat in the Garden 2[求点到线段的距离]
- ural 1804 The Machinegunners in a Playoff
- ural 1084 Goat in the Garden
- Ural1084. Goat in the Garden
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)