(简单)搜索 HOJ 1030 Labyrinth

Labyrinth

My Tags  (Edit)
Source : ACM ICPC Central European Regional 1999
Time limit : 5 secMemory limit : 32 M

Submitted : 816, Accepted : 344

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 Specification

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 Specification

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:
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
Sample Output:
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 100000000
const 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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值