Ultraviolet (a) problem 705 slash maze (diagonal line maze)

Source: Internet
Author: User
Tags dot net
// Slash maze (diagonal line maze) // PC/Ultraviolet IDs: 110904/705, popularity: B, success rate: average level: 2 // verdict: accepted // submission date: 2011-09-28 // UV Run Time: 0.012 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // [Problem description] // you can get a small maze by filling a rectangle with a slash (/) and a backslash (\). For example: //// (because gedit cannot insert an image, please refer to the image of the original question on the ultraviolet A, the original question link: http://uva.onlinejudge.o // rg/index. PHP? Option = com_onlinejudge & Itemid = 8 & category = 37 & page = show_problem & pr // oblem = 646 .) /// As you can see, the paths in the Maze are not forked, so there is only (1) ring paths in the Maze and (2) A path that enters from one place and // goes out from another place. We are only interested in rings. In the preceding example, there are two rings. //// Your task is to write a program to calculate the length of the longest ring. The length of the ring is defined as the number of small squares that constitute the ring (marked with gray // lines in the figure. In the preceding example, the length of a long ring is 16 and that of a short ring is 4. //// [Input] // The input contains several groups of data. The first row of each data group contains two integers W and H (1 <= W, H <= 75), representing the width and height of the maze, respectively. The next row of H contains W characters, which describes the entire Maze. These characters are "/" or "\". //// When w = H = 0, the input ends. This line does not have to be processed. //// [Output] // For each maze, a line of "maze # N:" Is output first, where N is the number of the maze. Then, output a line "K cycles; the longest // has length L.", where k is the number of central rings in the maze, and L is the length of the longest ring. If there is no ring in the maze, output "there // are no cycles." // print a blank line after the output of each group of data. //// [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. //// [solution] // if it is a normal maze, it is better to Solve the Problem horizontally and vertically. However, this question is a labyrinth represented by a diagonal line, at first glance, it was not very easy to deal with. On the ground, the hidden graphs of the Maze composed of the diagonal lines and the backlash were constructed and converted into a straight-line maze, use the graph traversal algorithm //, flood fill, or union-find to calculate the maximum ring length. //// Observe the graph given by the question. The diagonal line and the backslash occupy the side of two small squares, so the side occupied by the diagonal line can be expressed as 1, the side occupied by the backslash is represented by 2, and the side not occupied is represented by 0. Then the graph given by the question is converted to the following matrix: /// 200101202001 // 021010020210 // 200101012001 // 021010100210 // 010120200120 // 101002021002 // 200120010101 // 021002101010 // The problem is converted to finding the slave value in this matrix 0 to return to the longest path of the departure position. First, find the position/0 from the outermost side of the matrix, and use the flood fill algorithm to set all the locations connected to the 0 position to 3, which indicates that these vertices have been processed, because // these locations cannot constitute a ring, this is obvious (if you don't understand it, think about it again ...). After all the 0 locations around the matrix are processed, the matrix is changed: /// 233131232331 // 321310023213 // 233101012331 // 321010100213 // 310120200123 // 101002021332 // 200120013131 // 321332131313 // after processing, all zero points that cannot be converted into loops have been processed. In this case, select any zero position from the matrix, use the // flood fill algorithm to traverse all locations that can be connected to the zero position, set it to 3, and record the number of times 0 is found in this process, // The length of the ring. //// The only thing you need to note is the problem of selecting the time history direction. Because the forward and backward slashes have been marked, you only need to go in the direction where the position is 0, // The feasible direction must be determined based on the adjacent forward and backward slashes. //// Flood fill algorithm Introduction: // http://en.wikipedia.org/wiki/flood_fill. /// Union-find algorithm Introduction: // http://www.cs.princeton.edu /~ RS/algsds07/01unionfind.pdf. # Include <iostream> using namespace STD; # define maxv 150 # define empty 0 # define slash 1 # define backslash 2 # define visited 3 # define left_up 0 # define up 1 # define right_up 2 # define left 3 # define right 4 # define left_bottom 5 # define down 6 # define right_bottom 7 struct mazes {int status [maxv] [maxv]; int width, height ;}; int length; int offset [8] [2] ={{-1,-1 },{-1, 0 },{-1, 1}, {0,-1}, {0, 1}, {1,-1}, {1, 0 },{ 1, 1 }}; // check whether the coordinates are in the matrix. Inline bool in_range (INT line, int row, int width, int height) {return (0 <= line & line 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.