首页 > 代码库 > UVA 705 Slash Maze
UVA 705 Slash Maze
题目如下:
Slash Maze
Slash Maze |
By filling a rectangle with slashes (/) and backslashes (), youcan generate nice little mazes. Here is an example:
As you can see, paths in the maze cannot branch, so the whole maze onlycontains cyclic paths and paths entering somewhere and leavingsomewhere 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 thelength of the longest one. The length is defined as the number ofsmall squares the cycle consists of (the ones bordered by gray linesin the picture). In this example, the long cycle has length 16 andthe short one length 4.
Input
The input contains several maze descriptions. Each description begins with oneline containing two integersw and h (), the width and the height of the maze. The nexth 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:‘‘, wheren is the number of the maze. Then, output the line``kCycles; the longest has lengthl.‘‘, wherek is the number of cycles in the maze andl 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.
斜线迷宫的题,初看真是无从下手。在网上看了看大神们的思路,顿时只有膜拜的份了。把斜线看成是数字,这样整个抽象的斜线图就可以转化成一个简洁明了的数字矩阵,再用flood_fill算法可以求出环的最大长度。将每条斜线看成一个2*2矩阵,/表示成1,\表示成2,空格表示成0代表路,已经访问过的标记为3.先将边界的0以及与边界的0相连的0用flood_fill算法先标记为3,因为这些0是不可能构成环的,自然剩下的0都能构成环。在用flood_fill算法的时候,对于上下左右的0,可以直接用flood_fill算法访问,但对于对角线上的0,要先判断。例如右上有0,那么右边不能为\,类似有四种情况。
AC的代码如下: