Easy-to-understand Dancing links (2)

Source: Internet
Author: User

Dancing Links are generally used to solve the precise coverage problem. What is the precise coverage problem? This is the following:

Precise coverage of DancingLinks

Description

For the following 01 matrix, select several rows so that each column of the Matrix has only one

Lines 1, 4, and 5 are exactly covered. For details about how to use the Dancing Link to obtain this solution, refer to the code.

Http://blog.csdn.net/acmer1183/article/details/6320437

Next, I will focus on explaining the specific dance steps and processes of dance links for this question, that is, the detailed explanation of the above reference procedures.

First, select two rows and one column (that is, the row where the green box is located). The effect is that and 7 columns are deleted, and 2, 4, 5, and 6 rows are also deleted.

X

X

X
0 0 1 0 1 1 0
1 0 0 1 0 0 1 X
0 1 1 0 0 1 0
1 0 0 1 0 0 0 X
0 1 0 0 0 0 1 X
0 0 0 1 1 0 1 X

The colored columns in the figure indicate that these columns have been deleted, and the rows in the green box indicate the rows selected currently.

Columns 2, 3, 5, and 6 still need to be deleted.

We choose to delete a column that contains at least 1, and now the column that contains at least 1 is 2nd

X X X X
X X
0 0 1 0 1 1 0 X
1 0 0 1 0 0 1 X
0 1 1 0 0 1 0 X
1 0 0 1 0 0 0 X
0 1 0 0 0 0 1 X
0 0 0 1 1 0 1 X

At this time, only 5th columns have not been deleted, because 5th columns do not have 1 (originally 5th columns have two 1, but they are deleted in step 1 and step 2, respectively, are dyed blue and green respectively.

During backtracking, because the cells in the columns of the 3rd rows and 2 columns do not have any sibling nodes, that is, the column where the cell is located has only one

Here we will explain the "time": Originally, cells in three rows and two columns (green cells) had a sibling node (5 rows and 2 columns of cells,

However, this brother node has been removed from the dancing step and dyed blue because we have selected one column and two rows.

Sibling relationship: cells with values of 1 in different rows in the same column are in the same color, so that these cells have a sibling relationship.

If you find that there is no solution in the selection of 2 or 3 rows, you can start to trace back directly to the following:

Continue dancing and select columns with at least 1

Continue dancing and select columns with at least 1

Now, all columns are deleted. Therefore, selecting 1, 4, and 5 rows is a solution for exact coverage.

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.