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,如需转载请自行联系原作者

相关文章
|
C语言
程序设计基础课程描述
程序设计基础课程描述
209 0
|
机器学习/深度学习 数据采集 自然语言处理
Pandas数据应用:情感分析
本文介绍了如何使用Pandas进行情感分析,涵盖数据准备、清洗、特征工程和模型构建。通过读取CSV文件、处理缺失值与重复项、转换文本格式,利用TF-IDF提取特征,并采用SVM等算法训练分类器。还讨论了内存不足、过拟合等问题的解决方案。旨在帮助读者掌握情感分析的基本流程与技巧。
330 35
|
JSON JavaScript 测试技术
掌握JMeter:深入解析如何提取和利用JSON数据
Apache JMeter教程展示了如何提取和使用JSON数据。创建测试计划,包括HTTP请求和JSON Extractor,设置变量前缀和JSON路径表达式来提取数据。通过Debug Sampler和View Results Tree监听器验证提取结果,然后在后续请求和断言中使用这些数据。此方法适用于复杂测试场景,提升性能和自动化测试效率。
|
缓存 JavaScript 前端开发
三分钟,带你学会 Vue3 加载远程组件
三分钟,带你学会 Vue3 加载远程组件
WK
|
数据安全/隐私保护
QLineEdit
QLineEdit是Qt框架中的单行文本输入框控件,支持文本输入、占位符、密码模式、输入限制等功能。常用成员函数包括设置文本、占位符、显示模式、最大长度等。提供多种信号,如文本变化、编辑、回车等。支持添加动作和清除按钮,可定制样式,适用于登录、搜索等场景。
WK
597 0
|
机器学习/深度学习 算法 TensorFlow
【深度学习】深度学习语音识别算法的详细解析
深度学习语音识别算法是一种基于人工神经网络的语音识别技术,其核心在于利用深度神经网络(Deep Neural Network,DNN)自动从语音信号中学习有意义的特征,并生成高效的语音识别模型。以下是对深度学习语音识别算法的详细解析
1026 5
|
消息中间件 缓存 负载均衡
构建高效可靠的后端系统架构
本文将探讨如何构建一种高效可靠的后端系统架构,以满足不断增长的技术需求和用户期望。我们将重点介绍架构设计原则、分布式系统、容错机制和性能优化等关键概念,并提供实际案例和最佳实践,帮助开发者在后端开发中取得成功。
|
Linux 数据安全/隐私保护
基于阿里云服务器使用宝塔面板搭建 Typecho 博客
基于阿里云服务器使用宝塔面板搭建 Typecho 博客
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp微信小程序的在线考试系统的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的在线考试系统的详细设计和实现
238 2
|
存储 数据挖掘 数据处理
使用pandas高效读取筛选csv数据
本文介绍了使用Python的Pandas库读取和处理CSV文件。首先,确保安装了Pandas,然后通过`pd.read_csv()`函数读取CSV,可自定义分隔符、列名、索引等。使用`head()`查看数据前几行,`info()`获取基本信息。Pandas为数据分析提供强大支持,是数据科学家的常用工具。

热门文章

最新文章