| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 586 人关注过本帖
标题:变向的深度优先搜索
只看楼主 加入收藏
jing0128
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-8-24
收藏
 问题点数:0 回复次数:0 
变向的深度优先搜索

Labyrinth
TimeLimit : 10 Second Memorylimit : 32 Megabyte
Totalsubmit : 48 Accepted : 13


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. 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.


Sample 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 X is 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.
我个人觉得此题就是一个深度朝四个方向的深度优先搜索,我的代码如下,但是有问题.问题出在进行了一次深搜之后没有进行下次深搜,也就是说出的结果就是第一次深搜的结果,无法得到步数最大的.请大家指点一下.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char s[1001][1001];
int step,r,c,max;
int dir[4][2]={{0,1},{-1,0},{1,0},{0,-1}};
bool sign;
void DFS(int x,int y)
{
int i,j;
sign=false;
for(i=0;i<4;i++)
{
int x1=x+dir[i][0];
int y1=y+dir[i][1];
if(x1>=0&&x1<r&&y1>=0&&y1<c&&s[x1][y1]=='.')
{
printf("a\n");
sign=true;
s[x1][y1]='#';//对当前接点标记为已搜索过
x=x1;
y=y1;
step++;
DFS(x,y);
s[x][y]='.';//返回之后该点也作为没有用过的
}
}
if(!sign)//对每次深搜完毕之后做处理
{
if(max<step)
max=step;
step--;//因为周围都不能走,回到上一层,所以步数自减;
return ;
}
}
int main()
{
int Case;
scanf("%d",&Case);
while(Case--)
{
scanf("%d%d",&c,&r);
int i,j;
for(i=0;i<r;i++)
scanf("%s",s[i]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(s[i][j]=='.')
goto next;
next:
step=0;
s[i][j]='#';//i,j为起始接点
max=0;
DFS(i,j);
printf("Maximum rope length is %d.\n",max);
}
system("pause");
return 0;
}

搜索更多相关主题的帖子: 优先搜索 深度 
2007-08-28 10:19
快速回复:变向的深度优先搜索
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016420 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved