705 - Slash Maze

简介:

By filling a rectangle with slashes (/) and backslashes ( $\backslash$), 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 integersw and h ( $1 \le w, h \le 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.
#include <cstdio>
#include <cstring>

char maze[150][150];
int visit[150][150];
int m,n,length;

void findCircle(int i,int j)
{
	if(i<0 || j<0 || i>=2*n || j>=2*m)
	{
		length=0;
		return;
	}
	if(maze[i][j]!=0 || visit[i][j]==1)
		return;
	visit[i][j]=1;
	length++;
	findCircle(i-1,j);
	findCircle(i,j-1);
	findCircle(i,j+1);
	findCircle(i+1,j);
	if(!(maze[i-1][j]=='/' || maze[i][j-1]=='/'))
		findCircle(i-1,j-1);
	if(!(maze[i+1][j]=='/' || maze[i][j+1]=='/'))
		findCircle(i+1,j+1);
	if(!(maze[i+1][j]=='\\' || maze[i][j-1]=='\\'))
		findCircle(i+1,j-1);
	if(!(maze[i][j+1]=='\\' || maze[i-1][j]=='\\'))
		findCircle(i-1,j+1);
}

int main()
{
	int maxLength,count,num=0;
	while(scanf("%d%d",&m,&n)==2 && m!=0)
	{
		num++;
		maxLength=count=0;
		memset(maze,0,sizeof(maze));
		memset(visit,0,sizeof(visit));
		getchar();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				char c=getchar();
				if(c=='/')
				{
					maze[i*2][j*2+1]='/';
					maze[i*2+1][j*2]='/';
				}
				if(c=='\\')
				{
					maze[i*2][j*2]='\\';
					maze[i*2+1][j*2+1]='\\';
				}
			}
			getchar();
		}
		for(int i=0;i<n*2;i++)
			for(int j=0;j<m*2;j++)
			{
				if(!maze[i][j] && !visit[i][j])
				{
					length=0;
					findCircle(i,j);
					if(length!=0)
						count++;
					if(maxLength<length)
						maxLength=length;
				}
			}
			printf("Maze #%d:\n",num);
			printf(count==0?"There are no cycles.\n\n":"%d Cycles; the longest has length %d.\n\n",count,maxLength);
	}
	return 0;
}






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5278076.html,如需转载请自行联系原作者

目录
打赏
0
0
0
0
56
分享
相关文章
C++ 中的 std::next_permutation 和 prev_permutation
它用于将范围 [first, last) 中的元素重新排列为下一个字典序更大的排列。一个排列是 N! 元素可以采用的可能排列(其中 N 是范围内的元素数)。不同的排列可以根据它们在字典上相互比较的方式进行排序。代码的复杂度为 O(n*n!),其中还包括打印所有排列。
157 0
CF1365D Solve The Maze (BFS)
CF1365D Solve The Maze (BFS)
108 0
CF1365D Solve The Maze (BFS)
[LeetCode]--60. Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): 1."123" 2."132" 3
1092 1
[LeetCode]--73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution us
1005 0
[LeetCode]--59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9
1160 0
[LeetCode]--54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9
962 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等