UVA 705 Slash Maze

來源:互聯網
上載者:User

標籤:uva   data structure   graph   acm   

題目如下:

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的代碼如下:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.