Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_ problem&problem=646
Topic Type: Search
Topic:
By filling a rectangle with slashes (/) and backslashes (), the can generate nice little mazes. This is example:
As you can-paths in the maze cannot branch, so whole maze only contains cyclic paths and paths entering 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 into the picture ). In this example, the long cycle has length and the short one length 4.
Translation of the title to the effect:
Fill in a rectangle with a slash and a backslash, and you can create a nice little maze. Here's an example:
As shown above, you can compose a maze, slash and backslash equivalent to a maze of walls, the path in the maze can not branch, so the entire maze contains only circular path closure (unable to get out of the maze),
And there are paths that lead to the maze.
Your task is to write a program that calculates how many closed paths (circular paths) The maze has formed, and how long the longest closed path is calculated.
Sample input:
6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
0 0
Sample output:
Maze #1:
2 Cycles longest has length.
Maze #2:
There are no cycles.
Analysis and Summary:
At first look at this problem, a little at a loss, mainly is the image given is not in accordance with the conventional, square is tilted, so, it is difficult to convert to use an array to save.
Later, I found that the question of the length of the oblique bar is a lattice of twice times longer, so it is natural to think of the original image to expand twice times, in the array of two lattice to store a slash.
In this case, you can use an array to represent the image.
The converted figure is as follows (slightly rubbed)
Of these, the lattice represented by the pentagram is empty (can be represented by a symbol), and these squares can be walked.
After the conversion, it is easy to use search to make this problem.
Where to pay attention to: search, you can go in 8 directions, of which, go up, down, left and right, if the grid is empty, then you can go directly to the past.
But if you go in a slanting direction, only empty is not enough to judge to walk, but also need to be a special sentence.