標籤:uva data structure graph acm
題目如下:
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的代碼如下: