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

  1. 6 4
  2. \//\\/
  3. \///\/
  4. //\\/\
  5. \/\///
  6. 3 3
  7. ///
  8. \//
  9. \\\
  10. 0 0

Sample Output

  1. Maze #1:
  2. 2 Cycles; the longest has length 16.
  3.  
  4. Maze #2:
  5. There are no cycles.
  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. char maze[150][150];
  5. int visit[150][150];
  6. int m,n,length;
  7.  
  8. void findCircle(int i,int j)
  9. {
  10. if(i<0 || j<0 || i>=2*n || j>=2*m)
  11. {
  12. length=0;
  13. return;
  14. }
  15. if(maze[i][j]!=0 || visit[i][j]==1)
  16. return;
  17. visit[i][j]=1;
  18. length++;
  19. findCircle(i-1,j);
  20. findCircle(i,j-1);
  21. findCircle(i,j+1);
  22. findCircle(i+1,j);
  23. if(!(maze[i-1][j]=='/' || maze[i][j-1]=='/'))
  24. findCircle(i-1,j-1);
  25. if(!(maze[i+1][j]=='/' || maze[i][j+1]=='/'))
  26. findCircle(i+1,j+1);
  27. if(!(maze[i+1][j]=='\\' || maze[i][j-1]=='\\'))
  28. findCircle(i+1,j-1);
  29. if(!(maze[i][j+1]=='\\' || maze[i-1][j]=='\\'))
  30. findCircle(i-1,j+1);
  31. }
  32.  
  33. int main()
  34. {
  35. int maxLength,count,num=0;
  36. while(scanf("%d%d",&m,&n)==2 && m!=0)
  37. {
  38. num++;
  39. maxLength=count=0;
  40. memset(maze,0,sizeof(maze));
  41. memset(visit,0,sizeof(visit));
  42. getchar();
  43. for(int i=0;i<n;i++)
  44. {
  45. for(int j=0;j<m;j++)
  46. {
  47. char c=getchar();
  48. if(c=='/')
  49. {
  50. maze[i*2][j*2+1]='/';
  51. maze[i*2+1][j*2]='/';
  52. }
  53. if(c=='\\')
  54. {
  55. maze[i*2][j*2]='\\';
  56. maze[i*2+1][j*2+1]='\\';
  57. }
  58. }
  59. getchar();
  60. }
  61. for(int i=0;i<n*2;i++)
  62. for(int j=0;j<m*2;j++)
  63. {
  64. if(!maze[i][j] && !visit[i][j])
  65. {
  66. length=0;
  67. findCircle(i,j);
  68. if(length!=0)
  69. count++;
  70. if(maxLength<length)
  71. maxLength=length;
  72. }
  73. }
  74. printf("Maze #%d:\n",num);
  75. printf(count==0?"There are no cycles.\n\n":"%d Cycles; the longest has length %d.\n\n",count,maxLength);
  76. }
  77. return 0;
  78. }

705 - Slash Maze的更多相关文章

  1. UVA 705 Slash Maze

     Slash Maze  By filling a rectangle with slashes (/) and backslashes ( ), you can generate nice litt ...

  2. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  3. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  4. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  5. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  6. Backtracking algorithm: rat in maze

    Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...

  7. 1Z0-053 争议题目解析705

    1Z0-053 争议题目解析705 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 705.View Exhibit1 to examine the DATA disk group ...

  8. Crontab中的除号(slash)到底怎么用?

    crontab 是Linux中配置定时任务的工具,在各种配置中,我们经常会看到除号(Slash)的使用,那么这个除号到底标示什么意思,使用中有哪些需要注意的地方呢?   在定时任务中,我们经常有这样的 ...

  9. (期望)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 ...

随机推荐

  1. oracle解析xml完成版

    SELECT * FROM XMLTABLE('$B/DEAL_BASIC/USER_DEAL_INFO' PASSING XMLTYPE('<?xml version="1.0&qu ...

  2. Xcode换版本或者改名字后无法使用simpholders2

    修改一下路径,在终端下输入下面的命令sudo /usr/bin/xcode-select -switch /Applications/Xcode.app/Contents/Developer 敲回车后 ...

  3. linux 命令及进程控制

    main.c  main.o/main.obj  main/main.exe          编译                连接 程序运行;      两步: gcc/g++  -c  mai ...

  4. POJ 2411.Mondriaan's Dream 解题报告

    题意: 给出n*m (1≤n.m≤11)的方格棋盘,用1*2的长方形骨牌不重叠地覆盖这个棋盘,求覆盖满的方案数. Solution:                位运算+状态压缩+dp       ...

  5. vi文本编辑器

    vi文本编辑器分为3个模式: 命令模式 插入模式 ex模式 在命令模式下我们可以使用一下功能 o 插入新的行 u 撤销 n yy  复制n行 p 粘贴 / 查找 i 进入插入模式 exc到命令模式 e ...

  6. Neutron/ML2学习

    Neutron/ML2 Neutron ML2 模块层2(ml2)插件是一种允许OpenStack网络同时地利用在复杂现实数据中心发现的各种第二层网络技术的框架.目前它与存在的openvswitch. ...

  7. LINUX系统安装MYSQL命令,纯手打

    1.下载安装包 wget http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz    2. ...

  8. MySql奇葩问题汇总

    当字段名与关键词重叠时,sql语句中用``将字段名括起来,就可解决报错的问题.

  9. java-base64

    1.encode public static String encode(byte[] bValue, String encode) { ByteArrayOutputStream o = new B ...

  10. 利用Php ssh2扩展实现svn自动提交到测试服务器

    1.安装ssh2扩展 (1)window . 下载 php extension ssh2 下载地址 http://windows.php.net/downloads/pecl/releases/ssh ...