ZOJ 1142 Maze(dfs)

Maze

Time Limit: 2 Seconds       Memory Limit: 65536 KB

By filling a rectangle with slashes (/) and backslashes (\), you can generate nice little mazes. Here is an example:

As you can see, paths in the maze cannot branch, so the whole maze only contains cyclic paths and paths entering somewhere and leaving somewhere else. We are only interested in the cycles. In our example, there are two of them.

Your task is to write a program that counts the cycles and finds the length of the longest one. The length is defined as the number of small squares the cycle consists of (the ones bordered by gray lines in the picture). In this example, the long cycle has length 16 and the short one length 4.


Input 

The input contains several maze descriptions. Each description begins with one line containing two integers w and h (1 <= w,h <= 75), the width and the height of the maze. The next h lines represent the maze itself, and contain w characters each; all these characters will be either ``/" or ``\".

The input is terminated by a test case beginning with w = h = 0. This case should not be processed.


Output 

For each maze, first output the line ``Maze #n:'', where n is the number of the maze. Then, output the line ``kCycles; the longest has length l.'', where k is the number of cycles in the maze and l the length of the longest of the cycles. If the maze does not contain any cycles, output the line ``There are no cycles.".

Output a blank line after each test case.


Sample Input

6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
0 0


Sample Output

Maze #1:
2 Cycles; the longest has length 16.

Maze #2:
There are no cycles.


Source:  Mid-Central European Regional Contest 1999


本题主要两个难点:建立合适的数据结构存储迷宫;判断循环路线。下述代码是判断循环路线的代码,存储迷宫代码略去。


代码:

int wall[N][4];//保存迷宫中方格的墙 
int next[N][4];//存储相邻方格编号 

int dfs(int i,int length,int start)//i当前方格编号,start起始方格编号 
{
	path[i] = length;
	//搜索到起始结点,是一条循环路线 
	if (i==start && length>2) return 1;
	//不是循环路线,有分支路线 
	if (used[i]) return 0;
	//标记访问 
	used[i]=1;
	//如果没有墙,进入下一个方格 
	if (!wall[i][0] && dfs(next[i][0],length+1,start)) return 1;
	if (!wall[i][1] && dfs(next[i][1],length+1,start)) return 1;
	if (!wall[i][2] && dfs(next[i][2],length+1,start)) return 1;
	if (!wall[i][3] && dfs(next[i][3],length+1,start)) return 1;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值