Hdu 1426 Sudoku Killer (precise coverage of Dancing Link), hdusudoku

Source: Internet
Author: User

Hdu 1426 Sudoku Killer (precise coverage of Dancing Link), hdusudoku

Using the Dancing Link to solve Sudoku

For details, refer to the lrj training guide andDancing LinkS application in search


Dancing Link is used to solve the problem of data independence, which is precisely covered by the solution.

Precise coverage is to give a 01 matrix, requiring us to select some rows so that each column has only one


For the sudoku problem, the row is our choice, that is, to put the number k in column j of row I, so we can select at most from column I * j * k.

If the number a has been placed in some locations (x, y), we can only choose (x, y, a). Otherwise, we can choose (x, y, 1 ~ 9)


For columns, that is, the conditions we need to meet, there are four main types of conditions:

1. column y of row x must have numbers

2. Line x must have a number y

3. column x must have a number y

4. The x th palace must have the number y

Therefore, there are 9*9*4 columns in total (assuming 9*9*4 is the sudoku)


Then we only need to perform the DLX operation.


# Include <stdio. h> # include <string. h >#include <algorithm> # include <vector> using namespace std; const int maxn = 9*9*4 + 10; const int maxr = 9*9*9 + 10; const int maxnode = maxr * 4 + maxr + 10; # define FOR (I, A, s) for (int I = A [s]; I! = S; I = A [I]) struct DLX {// Number of maxn columns, total number of maxnode nodes, number of maxr rows int n, sz; int S [maxn]; int row [maxnode], col [maxnode]; int L [maxnode], R [maxnode], U [maxnode], D [maxnode]; int H [maxr]; int ansd, ans [maxr]; void init (int N) {n = N; // The virtual node of the first line for (int I = 0; I <= n; I ++) {U [I] = D [I] = I; L [I] = I-1; R [I] = I + 1 ;} R [n] = 0; L [0] = n; sz = n + 1; // number of each column memset (S, 0, sizeof (S); // H [I] =-1 indicates that this row does not have 1 // otherwise, it indicates the number of memset (H, -1, sizeof (H);} // Add a 1 void Link (int r, int c) {row [sz] = r in Column c of row r; col [sz] = c; S [c] ++; D [sz] = c; U [sz] = U [c]; D [U [c] = sz; U [c] = sz; if (H [r] <0) {H [r] = L [sz] = R [sz] = sz;} else {R [sz] = H [r]; L [sz] = L [H [r]; R [L [sz] = sz; L [R [sz] = sz;} sz ++ ;} // delete column c void remove (int c ){// Delete column c [R [c] = L [c]; R [L [c] = R [c]; // traverse FOR (I, D, c) down from column c {// Delete the row FOR (j, R, I) {D [U [j] = D [j]; U [D [j] = U [j]; -- S [col [j] ;}} // restore column c void restore (int c) {FOR (I, U, c) {FOR (j, L, I) {++ S [col [j]; U [D [j] = D [U [j] = j ;}} L [R [c] = R [L [c] = c;} bool dfs (int d) {// if no Column exists, algorithm end if (R [0] = 0) {ansd = d; return true;} // find the smallest s To speed up the search. int c = R [0]; FOR (I, R, 0) {if (S [I] <S [c]) c = I ;} // delete column c remove (c); // traverse the rows with 1 in the selected column FOR (I, D, c) {ans [d] = row [I]; // Delete the column FOR (j, R, I) {remove (col [j]);} if (dfs (d + 1) return true; // reply to the deleted column FOR (j, L, I) {restore (col [j]) ;}} restore (c); return false ;} bool solve () {if (! Dfs (0) return false; return true ;}} dlx; int ans [15] [15]; const int SLOT = 0; const int ROW = 1; const int COL = 2; const int SUB = 3; inline int encode (int a, int B, int c) {return a * 9*9 + B * 9 + c + 1;} int main () {char str [5]; int casn = 0; while (scanf ("% s", str )! = EOF) {if (str [0] = '? ') {Ans [0] [0] = 0;} else {ans [0] [0] = str [0]-'0 ';} for (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {if (I = 0 & j = 0) continue; scanf ("% s", str); if (str [0] = '? ') Ans [I] [j] = 0; else ans [I] [j] = str [0]-'0' ;}} if (casn ++) {puts ("");} dlx. init (9*9*4); for (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {for (int k = 0; k <9; k ++) {int rr = encode (I, j, k ); if (ans [I] [j] = 0 | ans [I] [j] = k + 1) {dlx. link (rr, encode (SLOT, I, j); dlx. link (rr, encode (ROW, I, k); dlx. link (rr, encode (COL, J, k); dlx. link (rr, encode (SUB, (I/3) * 3 + j/3, k) ;}}} dlx. solve (); for (int I = 0; I <dlx. ansd; I ++) {int r, c, v; int t = dlx. ans [I]; t --; v = t % 9; t/= 9; c = t % 9; t/= 9; r = t; ans [r] [c] = v + 1 ;}for (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {printf (j = 8? "% D \ n": "% d", ans [I] [j]) ;}} return 0 ;}






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.