Dance chain algorithm and Sudoku solution

Source: Internet
Author: User
Tags first row
Dance chain algorithm and Sudoku solution

Dance Chain Algorithm

The dance chain algorithm is used to solve the exact coverage problem.
The exact coverage problem can be understood as follows:
Given a 01 matrix, look for a collection of rows, so that the columns in the collection exactly match each column with a 1.

Example: Matrix
⎛⎝⎜⎜⎜⎜⎜⎜01010001010011010101⎞⎠⎟⎟⎟⎟⎟⎟\begin{equation} \left (\BEGIN{ARRAY}{CCCC} 0 & 0 & 0 & 1\\ 1 & 0 &am P 0 & 0\\ 0 & 1 & 1 & 1 \ 1 & 0 & 1 & 0 \ 0 & 1 & 0 & 1 \end{array} \right) \end{e Quation}

A simple algorithm idea is that the first step is to select a row first (assuming the first row)
The fourth column in the first row contains a 1, and the column also contains 1 of rows 3rd and fifth. is the row that conflicts with the last selection.
In the matrix to remove the above column (first row, third row, row five, fourth column), the following matrix is obtained.
(110001) \begin{equation} \left (\BEGIN{ARRAY}{CCC} 1 & 0 & 0 \ 1 & 0 & 1 \end{array} \right) \end{equati On
Repeat the above algorithm, select the first row, remove the conflict after the discovery of an empty matrix. Check the matrix before removal, there is no row for the full 1. proves that the last choice was wrong. To backtrack.
For the original matrix, this time select the second row, remove the conflicting rows and then get the matrix.
⎛⎝⎜⎜⎜001100101011⎞⎠⎟⎟⎟\begin{equation} \left (\BEGIN{ARRAY}{CCCC} 0 & 0 & 1\\ 0 & 0 & 0\\ 1 & 1 & 1 \ 1 & 0 & 1 \end{array} \right) \end{equation}
For this matrix, try it in turn. When the third row is selected, a null matrix is obtained after the conflict is removed, and a total of 1 rows exist before removal. Get the results.

So the solution idea can be summed up as
1. Select a row
2. Remove conflicting rows to get a new matrix
3. If the matrix is empty, then the row before the removal is determined whether there is a total of 1, if any, to obtain the result. If it does not exist, go back and skip to step 1. If no matrix can be traced, the output does not exist

There are a number of matrix caches that may be faced when backtracking occurs. The dance chain algorithm solves this problem effectively.
The data structure used in the dance chain algorithm is a two-way cross loop linked list .
There are 6 attributes for each element, left pointing to the element on the right, point to the element at the top , up pointing to the element above, down to the following element, andCol indicating the column. row indicates where.
The advantage of this is that we don't need a lot of cache matrices when we're doing backtracking.
When deleting an element, it is assumed that the same row has three elements a, B, C, and B must be removed, only a. right = B.right,c.left = B.left.
When recovering an element, just find B. Left points to element A, so that a. right = B, find B. Right points to element C, so c. left = B.

According to this idea, the above mentioned matrix can be converted into a list of the following

⎛⎝⎜⎜⎜⎜⎜⎜01010

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.