Title Description
Description
Give a n*n (n<=100) chess board, in which some points were removed and asked how many 1*2 dominoes could be used to cover up.
Enter a description
Input Description
First Behavior n,m (indicates a lattice with M delete)
The second line to m+1 acts X, Y, respectively, indicating where the delete lattice is located
X is line X
Y is the nth column
Output description
Output Description
A number, that is, the maximum number of overlay cells
Sample input
Sample Input
: R
Sample output
Sample Output
32
Data range and Tips
Data Size & Hint
Classic Questions
Ideas
It is easy to see that this is a binary graph matching problem, is a good way to practice the Hungarian algorithm. To stain the chessboard, a domino must cover a black lattice and a white lattice. The black lattice and the white lattice are divided into two sets, that is, the binary graph model, the maximum matching can be obtained. The key is how to abstract the chessboard into points and establish connections. An understanding of the Hungarian algorithm is attached below.
Understanding of binary map matching and Hungarian algorithm
{Reprint first indicates source: http://blog.csdn.net/pi9nc/article/details/11848327}
binary diagram : Simply put, if the midpoint of the graph can be divided into two groups, and all edges cross the boundary of the group, then this is a two-part graph. To be exact: divide the vertex of a graph into two disjoint sets u and V, so that each edge is connected to the vertices in U and V, respectively. If such a division exists, the graph is a two-part graph. An equivalent definition of a binary graph is: A graph that does not contain "rings with odd numbers of edges". Figure 1 is a two-part diagram. In order to be clear, we will later draw it into the form of Figure 2.
match : In graph theory, a "match" (matching) is a set of edges in which any two edges have no public vertices. For example, the red edge in Figure 3, Figure 4, is the match of Figure 2.
We define matching points , matching edges , unmatched points , mismatched edges , and they are very obvious. Example 3, 1, 4, 5, 7 is the matching point, the other vertices are unmatched points, 1-5, 4-7 is the matching edge, the other edges are non-matching edges.
Maximum match : A match with the largest number of matched edges in all matches of a graph, called the maximum match for this graph. Figure 4 is a maximum match that contains 4 matching edges.
Perfect Match : if one of the graphs has a match, all vertices are matching points, then it is a perfect match. Figure 4 is a perfect match. Obviously, the perfect match must be the maximum match (any point of the perfect match has already been matched, adding a new matching edge will certainly conflict with the existing matching edge). But not every diagram has a perfect match.
-------, wait, look at the big head? Then see the following version:
{Reprint first indicates source: http://blog.csdn.net/dark_scope/article/details/8880547}
Through the efforts of several generations, you finally caught up with the tide of the remaining male, assuming you are a glorious new century matchmaker, in your hand there are n left male, m a woman, everyone may have a good impression on multiple heterosexual (-_-| | Temporarily regardless of special sexual orientation), if a pair of men and women, then you can put together the pair, now let us ignore all the unrequited love (a good feeling of sadness), you have is probably the following a diagram, each line is a mutual goodwill.
In the spirit of saving a life, the principle of building a seven-storey pagoda, you want to match as many couples as possible, the Hungarian algorithm working mode will teach you to do this:
===============================================================================
First : try to find the girl number 1th boys, found the first and he connected to the number 1th girls are still flowers, got it, connected to a blue line
===============================================================================
two : then to the 2nd boys to find a sister, found the first and he connected to the 2nd girl is no master, got it
===============================================================================
Three : Next is the number 3rd boys, it is very regrettable that the number 1th girls already have the Lord, how to do?
We tried to assign another sister to the boy who was a match for girl number 1th (aka Number 1th).
(yellow means the side is temporarily torn down)
The second girl with number 1th is the number 2nd girl, but the number 2nd girl also has the Lord, how to do? We're going to try to get a girl for number 2nd again. (Note this step is the same as above, this is a recursive process)
At this time found 2nd boys can still find the number 3rd girl, then the problem solved, go back
Number 2nd boys can find 3rd sister, ~ ~ 1th Boys can find 2nd sister, ~ ~ ~ 3rd Boys can find the number 1th sister
So the final result of the third step is:
===============================================================================
Four : Next is the number 4th boys, unfortunately, according to the rhythm of the third step we can not give the number 4th boys to free a sister, we really do not have the means. Fragrant custard classmate walk well.
===============================================================================
This is the Hungarian algorithm of the process, in which looking for a sister is a recursive process, the most critical word is "teng" word
The principle is: there is a chance, no chance to create a chance to go on
The basic concept is finished. One of the algorithms for solving the maximum matching problem is the Hungarian algorithm , and the concepts below are for this algorithm service.
Alternate Path : From an unmatched point, followed by a non-matching edge, matching edge, non-matching edge ... The formed path is called alternating road.
Augmented Road : From an unmatched point, take the alternate road, if the way another unmatched point (the point of departure does not count), then this alternate road is called the augmented path (agumenting path). For example, Figure 5 shows an augmented path of 6 (the matching points in the figure are marked in red):
There is an important feature of the augmented path: a non-matching edge is more than a matching edge. Therefore, the significance of studying the augmented path is to improve the matching . Simply swap the identities of the matching and non-matching edges in the augmented path. This does not break the matching character because there are no other matching edges in the middle of the matching node. After the swap, the number of matching edges in the figure is 1 more than the original one.
We can add matching edges and matching points in the match by constantly looking for the augmented path. When the augmented path is not found, the maximum match is reached (this is the augmented path theorem). This is exactly what the Hungarian algorithm does. Before you give the Hungarian algorithm DFS and BFS versions of the code, first talk about the Hungarian tree.
The Hungarian tree is generally constructed from BFS (similar to the BFS tree). Run BFS from an unmatched point (the only limitation is that you must take the alternate path) until you can no longer expand. For example, from Figure 7, you can get 8 of a BFS tree:
This tree has a leaf node as a non-matching point (number 7th), but the Hungarian tree requires all leaf nodes to be the matching points, so this is not a Hungarian tree. If the original image does not contain the number 7th node, then a Hungarian tree will be obtained from node 2nd. This scenario is shown in 9 (by the way, in Figure 8, the root node 2 to the non-matching leaf node 7 is obviously an augmented path that will be a perfect match along the augmented path).
Code
Don't give
View Code
Checkerboard Overlay and Hungarian algorithm