poj 1383 Labyrinth【bfs+队列】
来源:互联网 发布:男神的偶像知乎 编辑:程序博客网 时间:2024/05/17 04:38
Labyrinth
Time Limit: 2000MS Memory Limit: 32768KTotal Submissions: 4184 Accepted: 1564
Description
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. The ACM have found that two of the hooks must be connected by a rope that runs through the hooks in every block on the path between the connected ones. When the rope is fastened, a secret door opens. The problem is that we do not know which hooks to connect. That means also that the neccessary length of the rope is unknown. Your task is to determine the maximum length of the rope we could need for a given labyrinth.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers C and R (3 <= C,R <= 1000) indicating the number of columns and rows. Then exactly R lines follow, each containing C characters. These characters specify the labyrinth. Each of them is either a hash mark (#) or a period (.). Hash marks represent rocks, periods are free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks are blocks sharing a common side. We cannot walk diagonally and we cannot step out of the labyrinth.
The labyrinth is designed in such a way that there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is easy to find the right path connecting them.
The labyrinth is designed in such a way that there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is easy to find the right path connecting them.
Output
Your program must print exactly one line of output for each test case. The line must contain the sentence "Maximum rope length is X." where Xis the length of the longest path between any two free blocks, measured in blocks.
Sample Input
23 3####.####7 6########.#.####.#.####.#.#.##.....########
Sample Output
Maximum rope length is 0.Maximum rope length is 8.
Hint
Huge input, scanf is recommended.
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc
Source
Central Europe 1999
题意:
给你一条" . "和“ # ”字符构成的图,在这个图上只有一个由“ . ”连通图,问这个连通图中最长的一条路径;
思路:
树的直径的题目,只是这是个二维的图(用前向星存下来可能没那么方便),用矩阵存一下,然后bfs+queue按照数的直径的思路深搜下去;
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>#define max_n 1100using namespace std;typedef long long LL;int vis[max_n][max_n];char mapp[max_n][max_n];int n, m, maxn, ans1, ans2;int mov[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};struct node {int a;int b;int step;};void bfs(int x, int y) {queue<node> q;memset(vis, 0, sizeof(vis));vis[x][y] = 1;node now, next;now.a = x;now.b = y;now.step = 0;q.push(now);ans1 = 0, ans2 = 0, maxn = 0;while(!q.empty()) {next = q.front();q.pop();for(int i = 0; i < 4; i++) {now.a = next.a + mov[i][0];now.b = next.b + mov[i][1];if(!vis[now.a][now.b] && now.a >= 1 && now.a <= n && now.b <= m && now.b >= 1 && mapp[now.a][now.b] == '.') {vis[now.a][now.b] = 1;now.step = next.step + 1;if(now.step > maxn) {maxn = now.step;ans1 = now.a;ans2 = now.b;}q.push(now);}}}}int main() {int t, nx, ny;scanf("%d", &t);while(t--) {scanf("%d %d", &m, &n);for(int i = 1; i <= n; i++) {scanf("%s", mapp[i]); //如果一个一个字符输入的话会TLE,玄学 for(int j = 1; j <= m; j++) {if(mapp[i][j] == '.') {nx = i;ny = j;}}}bfs(nx, ny);bfs(ans1, ans2);printf("Maximum rope length is %d.\n", maxn);}return 0;}
阅读全文
1 0
- poj 1383 Labyrinth【bfs+队列】
- poj 1383 Labyrinth bfs
- POJ 1383 --Labyrinth【BFS】
- POJ 1383 Labyrinth(双次BFS)
- POJ 1383 Labyrinth(BFS 树的直径)
- Poj 1383--Labyrinth【树的直径】【bfs】
- POJ 1383 Labyrinth (技巧BFS)
- POJ 1383 Labyrinth(两次bfs求最长路径)
- POJ 1383 Labyrinth
- poj 1383 Labyrinth
- POJ 1383 Labyrinth
- POJ-1383 Labyrinth
- POJ 1383 Labyrinth
- 【POJ 1383 】Labyrinth
- 【POJ】[1383]Labyrinth
- 【POJ 1383 Labyrinth】
- POJ 1383 Labyrinth 笔记
- 【poj 1383】Labyrinth
- Python——切片备忘
- 最大差值(2017某计算机校招笔试试题)
- 百度地图自定义覆盖物及创建多个小图标
- HDU 6075 Questionnaire【】
- 数学、计算机及抽象思维
- poj 1383 Labyrinth【bfs+队列】
- 动态规划中级教程 322. Coin Change
- 如何把footer固定到页面最下端?
- fdfs防盗链配置及java开发
- [数据结构] 排序算法小结
- 二叉树的最大深度和最小深度实现
- 4.1.1—栈—Valid Parentheses
- php用curl获取sessionid
- HDU4704Sum 费马小定理+大数取模