Simulate test3 day1

Source: Internet
Author: User

Tile

Greedy

Question: a rectangle is given, which is filled with square represented by different letters. The numbers of adjacent square letters cannot be the same. The minimum solution is to calculate the Lexicographic Order (concatenate all rows.

Preliminary solution: I didn't think much at first, thinking that the Strategy is to fill a square as big as possible every time. However, we can quickly find the counterexample. For example, when the left half of a 4*2 rectangle is filled with a 2*2 A, the right half of the rectangle should not be filled with B of 2*2, instead, we should first fill in a 1*1 B, and then continue to fill it with ,.

In the above example, although C is used in the second scheme, the Lexicographic Order of the second scheme is obviously smaller after each row is spliced according to the meaning of the question.

Positive Solution: From the inverse example above, we can see that you can consider each grid in the order of each column in each row without considering the whole. For example, the first lattice (1, 1) in the upper left corner and the of 1*1 are undoubtedly the optimal solutions. Then, we can see whether it can be extended and find that it can be extended to a of 2*2, so we can fill it. Then the first lattice (1, 3) on the right of a should be filled with B of 1*1, and then check whether it can be expanded. The lattice (1, 4) can be filled with, therefore, B of (1, 3) is not extended. Each grid can be considered in the order of rows to minimize the Lexicographic Order.

?

Path

Shortest Path

Calculate the sum of all possible edges in an undirected graph between any two points.

Preliminary solution: it is obvious that Floyd is used to find the shortest path between two points. Then, I implemented a recursion of O (N ^ 3. F (I, j) indicates the number of edges in the shortest path from I to J. F (I, j) = sum {f (I, K), (K, j) ε E and dist (I, K) + g (K, J) = dist (I, j)}. The boundary condition is f (I, I) = 1. However, this method can find the inverse example, for example:

Assume that (1-> 3) and (1-> 4) in the figure are all part of the shortest path from 1 to a point, then F (2, 2) = 1 will be accumulated twice in the final result, but it is actually the same edge, so the answer will be too large.

Positive Solution: first understand the concept of the "Shortest Path. For any point (I, j), add the points and edges that have occurred in the shortest path from I to J to the shortest path from I to J. Obviously, the points in the shortest path must meet the dist (I, K) + dist (K, j) = dist (I, j) conditions. Then the total number of edges in all the shortest paths from I to J is equal to the inbound degree of each vertex in the shortest path. In a specific program implementation, you do not need to construct such a shortest path. You only need to determine whether the vertex meets the preceding conditions and then calculate its inbound level.

?

Tower

Query set

The side of a cylinder is given in the form of a matrix. Now we need to add obstacle points to it. The conditions for determining whether an obstacle point can be added are: after this obstacle point is added, there is still a four-connection path from the bottom up. Note that it is the side of the cylinder, so although given in the form of a matrix, there is no actual left and right boundary, that is, from the rightmost of the matrix can go to the leftmost of the matrix.

Preliminary solution: each time assuming that the obstacle points are added, and then DFS is performed on the bottom points to determine whether there is a four-connection path from the bottom to the top. It is said that if the optimization is good enough, it can also be AC.

positive solution: You will naturally think of and query the set for the connectivity judgment. However, the number of hollow grids in this question is constantly decreasing, and it is not easy to delete the elements in the query set. Therefore, clever conversion is required. For ease of processing, we can first completely copy the entire rectangle and coordinate the two rectangles. Add each vertex to two matrices. If a point and its "copy" point in another matrix form an eight-connected block through other obstacle points, the side of the whole cylinder will be blocked. You can figure it out by yourself. As the obstacles in the matrix are constantly increasing, it is easier to use and query sets. However, some skills are required to judge connectivity. In my first thought, I first added this point, and then merged it with the eight surrounding points and checked them in a centralized manner. However, if the result is that this vertex cannot be added, it is difficult to restore the query set to its original state. The idea is to enumerate the eight points around the points currently processed in the two matrices (Why do we only need to judge these eight points? If you add the current vertex, it will inevitably form the same eight connected blocks with the eight vertices next to it.) to determine whether the two vertices are in the same set, this requires 64 judgments. The better method is to use a hash table. In the matrix on the left, hash the eight vertices next to the currently processed vertex (the specific method is hash [find_set (I)] = xxx ), then hash is performed on the right side. If a conflict occurs during processing on the right side, it means that if this point is added, an eight-connected block spanning the side of the entire cylinder will be formed. Note: If the hash array is initialized to 0 for each processing point, the time consumption is very high. So we can use this optimization: Assign the hash value to the number of the currently processed vertex (that is, the cyclic variable I ). In this way, each hash will not be affected by the previous hash value, saving the time to reset the hash.

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.