Server Load balancer 705-Server Load balancer maze, diagonal line maze

Source: Internet
Author: User
705-slash maze 4016 40.19% 1120 83.66%

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 105 & page = show_problem & problem = 646

Question type: Search

Question:

By filling a rectangle with slashes (/) and backslashes (), you can generate nice
Little mazes. Here is an example:

As you can see, paths in the Maze cannot branch, so the whole maze only contains cyclic paths and paths entering somewhere 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 in
Picture). In this example, the long cycle has length 16 and the short one length 4.

Translation:

Fill in a rectangle with a slash and a backslash to generate a beautiful small maze. Here is an example:

For example, a labyrinth can be formed. The diagonal lines and the backslash are equivalent to the wall of the labyrinth. The path cannot be separated in the Maze. Therefore, the entire labyrinth only contains the closed loop path (which cannot be taken out of the maze ),

Other paths lead to the outside of the maze.

Your task is to write a program, calculate the number of closed paths (Cyclic paths) in the maze, and calculate the maximum number of closed paths. \


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.

Analysis and Summary:

When I first read this question, I was a little confused. The main difference was that the images were not placed in a regular way and the square was skewed. In this case, it was difficult to convert them into Arrays for storage.

Later, I found that the length of the Diagonal Bar given by the question was twice the length of a grid, so I naturally thought of doubling the original image, use two grids in the array to store a diagonal line.

In this way, we can use arrays to represent this image.

The following figure shows the converted image)

Five-star represents an empty lattice array (which can be expressed by a symbol.

After conversion, you can easily use search to make this question.

Note: When searching, you can go in eight directions. If the grid is empty, you can go up, down, left, and right.

However, if you are walking in the oblique direction, it is not enough to judge whether you can go, but you need to make a further decision.

For example, the following situations may occur:

If you go from top left to bottom right (or from bottom right to top left), though the bottom right is empty, you cannot go because there is a diagonal bar blocking the wall.

In addition to this method, we can also simulate the light reflection path to solve this problem (my teammates thought of this method), but it is more difficult to write the code.

Another method is to convert a single cell to a nine cell. After the conversion, search is the easiest to write, because no special conditions are required.

The code for converting 1 to 4 is as follows:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int W, H, last_x, last_y; char STR [100] [100], map [300] [300]; int dir [8] [2] = {}, {-}, {}, {0,-1 }, // This row is in the upper and lower directions: {-}, {}, {1,-1}, {-1,-1 }}; // This line is in the oblique direction, starting from the top left, clockwise bool vis [300] [300]; // convert the input into void change (INT row) {for (INT I = 0; I <strlen (STR [row]); ++ I) {int m_row = row * 2, m_col = I * 2; if (STR [row] [I] = '\') {map [m_row] [m_col] = '\'; Map [m_row + 1] [m_col + 1] = '\'; Map [m_row] [m_col + 1] = ''; map [m_row + 1] [m_col] = '';} else if (STR [row] [I] = '/') {map [m_row] [m_col + 1] = '/'; Map [m_row + 1] [m_col] = '/'; Map [m_row] [m_col] = ''; map [m_row + 1] [m_col + 1] = ''; }}// if it is oblique, You need to determine whether bool ispass (int x, int y, int dirno) {int X1, Y1, X2, Y2; // X1 is the same row, X2 is the same Col X1 = X, y1 = Y + dir [dirno] [1]; x2 = x + dir [dirno] [0], y2 = y; If (dirno = 4 | Dirno = 6) {If (Map [X1] [Y1] = '/' & map [X2] [y2] = '/') return true ;} else {If (Map [X1] [Y1] = '\' & map [X2] [y2] = '\') return true ;} return false;} void DFS (int x, int y, Int & CNT) {for (INT I = 0; I <8; ++ I) {int dx = x + dir [I] [0], dy = Y + dir [I] [1]; if (I <4) {// if it is up, bottom, left, right-click if (dx> = 0 & DX <2 * H & dy> = 0 & dy <2 * W & map [dx] [dy]! = '\' & Map [dx] [dy] = ''& map [dx] [dy]! = '/'&&! Vis [dx] [dy]) {vis [dx] [dy] = true; last_x = DX, last_y = Dy; // remember the position of the last inbound stack + + CNT; DFS (dx, Dy, CNT );}} else {// if it is in the oblique direction, if (DX <0 | DX> = 2 * H | dy <0 | dy> = 2 * w | map [dx] [dy] = '\' | map [dx] [dy] = '/' | Vis [dx] [dy]) continue; If (ispass (x, y, I) {vis [dx] [dy] = true; ++ CNT; last_x = DX, last_y = Dy; DFS (dx, dy, CNT) ;}}} int main () {# ifdef local freopen ("input.txt", "r", stdin); # endif int CAS = 1; whi Le (~ Scanf ("% d % * C", & W, & H) & W & H) {memset (STR, 0, sizeof (STR )); memset (MAP, 0, sizeof (MAP); memset (VIS, 0, sizeof (VIS); For (INT I = 0; I 


-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double



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.