705 - Slash Maze
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 integersw and h (
),
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;
}
705 - Slash Maze的更多相关文章
- UVA 705 Slash Maze
Slash Maze By filling a rectangle with slashes (/) and backslashes ( ), you can generate nice litt ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- 1Z0-053 争议题目解析705
1Z0-053 争议题目解析705 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 705.View Exhibit1 to examine the DATA disk group ...
- Crontab中的除号(slash)到底怎么用?
crontab 是Linux中配置定时任务的工具,在各种配置中,我们经常会看到除号(Slash)的使用,那么这个除号到底标示什么意思,使用中有哪些需要注意的地方呢? 在定时任务中,我们经常有这样的 ...
- (期望)A Dangerous Maze(Light OJ 1027)
http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...
随机推荐
- spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法
spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法 类的公共方法可以,但是私有方法不行 测试一下接口的方法是否能够捕捉到
- mac 查找当前目录下所有同一类型文件,并执行命令行
以TexturePacker举例 MAC下用TexturePacker命令行打包当前目录下所有的 *.tps文件 1.配置好tps文件需要配置好路径.参数等.(也可不配置,用命令行实现.具体参考:ht ...
- Eclipse4.4安装旧版本插件报错 Failed to prepare partial IU
Failed to prepare partial IU: [R]com.googlecode.veloeclipse.ui 2.0.8. work around: go to Help -> ...
- 一、初识T4引擎
对于代码生成器我们并不陌生,在日常编码中这也是用的比较多的工具之一.一般代码生成器主要功能是生成公共或基础代码来减少编码人员的工作量,而一款优秀的代码生成器除了生产代码以外,同时兼具生成项目架构和基础 ...
- cxf客户端代码wsdlLocation设置相对路径
利用工生成的cxf客户端代码,wsdlLocation都是绝对路径,为了便于项目更加灵活管理,我们可以将该路径设置为相对路径: 1.下面图片是我的项目路径图片及wsdl地址存放路径: 2.下面图片是我 ...
- SpringMVC介绍之约定优于配置
SpringMVC介绍之约定优于配置 所谓的约定优于配置就是指在程序开发过程中我们约定好一些规则可以使我们更少的进行配置和代码编写.就这么简单的一句话可能你还不是很懂什么是约定优于配置,没关系,看完后 ...
- C++拾遗(三)关于复合类型
数组相关 初始化只能在定义的时候使用,不能把数组赋给另一个数组. 初始化可以提供比元素数目少的初值,其它元素将被置为0. 字符char数组只有在以\0结尾时才是一个字符串.sizeof()返回数组的长 ...
- SxsTrace工具使用方法(转)
http://blog.sina.com.cn/s/blog_494e45fe0102dtt3.html Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库 ...
- [Git]Git远程仓库
1.创建ssh key 查看主目录下面 C:\Users\Administrator\.ssh 是否存在 id_rsa 和 id_rsa.pub 文件,如果不存在需要generate new key. ...
- win7 IIS7.0 【IIS 管理器无法验证此内置帐户是否有访问权】
异常信息: 服务器配置为将传递身份验证和内置帐户一起使用,以访问指定的物理路径.但是,IIS 管理器无法验证此内置帐户是否有访问权.请确保应用程序池标识具有该物理路径的读取访问权.如果此服务器加入到域 ...