(简单)搜索 HOJ 1030 Labyrinth
2014-02-08 09:51
344 查看
Labyrinth
My Tags | (Edit) |
---|
Source : ACM ICPC Central European Regional 1999 | |||
Time limit : 5 sec | Memory limit : 32 M |
#) 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 Specification[/b]Your program must print exactly one line of output for each test case. The line must contain the sentence "
Maximum rope length isX.where Xis the length of the longest path between any two free blocks, measured in blocks.Sample Input:[/b]
2 3 3 ### #.# ### 7 6 ####### #.#.### #.#.### #.#.#.# #.....# #######Sample Output:[/b]
Maximum rope length is 0. Maximum rope length is 8.
题意:给一张图,起点任选,找到一条路的长度是最长的思路:由于题目说了,图中任意两点都有且仅有一个路,所以可以直接先以任意一个点为起点,用dfs找到最长路,然后分别把这些最长路的终点作为起点,在进行一次dfs,就能得到真正的最长路了当然找最长路可以dfs也可以bfs , 都需要进行两次搜索操作,但是这个题目里面用dfs更快一些
代码:#include<iostream>#include<cstdio>#include<string.h>#include<algorithm>#include<string>#include<queue>using namespace std;#define MAX 1000+10#define MOD 100000000const int inf = 0x7fffffff;bool vis[1000010];char grid[MAX][MAX];int number[MAX][MAX];int d[MAX][MAX];
int ans;bool flag;void dfs(int r,int c,int step){ vis[number[r][c]] = true; if (flag) d[r][c] = step; if (step>ans) ans = step; if (grid[r+1][c]=='.' && !vis[number[r+1][c]]) { dfs(r+1,c,step+1); } if (grid[r-1][c]=='.' && !vis[number[r-1][c]]) { dfs(r-1,c,step+1); } if (grid[r][c+1]=='.' && !vis[number[r][c+1]]) { dfs(r,c+1,step+1); }
if (grid[r][c-1]=='.' && !vis[number[r][c-1]]) { dfs(r,c-1,step+1); }}
int main(){ int T; cin>>T; while (T--) { int C,R; scanf("%d%d",&C,&R); memset(grid,0,sizeof(grid)); for (int i = 1 ; i <= R ; ++i) scanf("%s",grid[i]+1); int cnt = 0; for (int i = 1 ; i <= R ; ++i) { for (int j = 1 ; j <= C ; ++j) if (grid[i][j]=='.') { number[i][j] = ++cnt; } } if (cnt==0) { printf("Maximum rope length is 0.\n"); continue; } memset(d,0,sizeof(d)); memset(vis,0,sizeof(vis)); ans = 0; flag = true; for (int i = 1 ; i <= R ; ++i) for (int j = 1 ; j <= C ; ++j) if (grid[i][j]=='.') { dfs(i,j,0); i = R+1; break; }
flag = false; for (int i = 1 ; i <= R ; ++i) { for (int j = 1; j <= C ; ++j) if (grid[i][j]=='.' && d[i][j]==ans) { memset(vis,0,sizeof(vis)); dfs(i,j,0); } } printf("Maximum rope length is %d.\n",ans); }}
相关文章推荐
- poj1111 hoj 1639 Image Perimeters 简单搜索
- (简单) 搜索 HOJ 1066 Substrings
- Go hdu 4158 hoj 简单搜索
- (简单)搜索 HOJ 1070 Word
- 【hoj】1030 Labyrinth
- HOJ 1440 Knight Moves -------简单搜索 BFS 求l两点之间最小的到达步数
- (简单)搜索 HOJ 1075 The Castle
- hdu 4158 hoj 2581 Go 简单搜索
- (简单) 搜索 HOJ 1085 Finding Rectangles
- (简单)搜索 HOJ 1692 Choose Your Own Adventure
- (简单) 搜索 HOJ 1097 Robot
- (简单) 搜索 HOJ 1105 Egyptian Multiplication
- (简单) 搜索 最短路 HOJ 1121 Erdos Numbers
- Hoj 1030 Labyrinth
- HOJ 1030 Labyrinth----------------两次BFS求树的直径(图的最长路)
- (简单)搜索 HOJ 1022 Spacecraft Malfunction
- HOJ 1030 && POJ 1383 - Labyrinth(树的直径、图的直径)
- (简单) 搜索 HOJ 1044 Transportation
- (简单) 搜索 HOJ 1048 Cipher
- 几句话描述简单算法——排序与搜索