POJ 1383 Labyrinth(双次BFS)
来源:互联网 发布:环球慧思海关数据登陆 编辑:程序博客网 时间:2024/05/16 19:47
Labyrinth
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 4159 Accepted: 1554
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.
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
2
3 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
本题的题意是:找出最远距离的两个’.’,并把他们的距离输出,
方法:从开始的点搜到一个最远的点,然后从这个最远的点再重新搜一次,为了防止找到的点不是最远的点。
AC代码如下:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;struct node{ int x,y,step;};char a[1005][1005];int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};int n,m,sx,sy;int book[1005][1005];int bfs(int x0,int y0){ int maxn1=0; queue<node>q; node fr,ne; fr.x=x0,fr.y=y0,fr.step=0; q.push(fr); while(!q.empty()) { fr=q.front(); q.pop(); for(int i=0; i<4; i++) { ne.x=fr.x+dir[i][0]; ne.y=fr.y+dir[i][1]; if(ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<m&&a[ne.x][ne.y]!='#'&&book[ne.x][ne.y]==0) { book[ne.x][ne.y]=1; ne.step=fr.step+1; if(ne.step>maxn1) { maxn1=ne.step; sx=ne.x,sy=ne.y; } q.push(ne); } } } return maxn1;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); for(int i=0; i<n; i++) { scanf("%s",&a[i]); } memset(book,0,sizeof(book)); int flag=0; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(a[i][j]=='.') { int re=bfs(i,j); flag=1; break; } } if(flag==1) break; } memset(book,0,sizeof(book));//为了新一次的搜索做准备 printf("Maximum rope length is %d.\n",bfs(sx,sy)); } return 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
- 阿里2016实习offer五面经验与总结
- 76. Minimum Window Substring
- maven集成测试
- SwipeRefreshLayout+RecyclerView 下拉刷新
- IOS学习 NSOperation 网络下载图片 涉及plist文件、重用Cell、占位图、缓存池、清理内存
- POJ 1383 Labyrinth(双次BFS)
- 杭电ACM题1007
- POJ 3522 Kruskal
- jQuery——小案例:点击图片放大缩小
- easyui + jquery 提交form表单 jquery submit失效
- Android学习之ScrollView使用文档翻译
- 拆分上中下div 且div中嵌套多个横排或竖排div
- spring(11)------spring国际化支持
- 网络:XML 解析之下拉刷新界面(手写代码添加)