Question:
16*16.
Train of Thought Analysis:
Needless to say, it is useless.
This is to say that the dancing links line is based on
Enter 1 in the first column of the first row.
Enter 2 in the second column of the first row.
......
Enter 15 in column 15th of the first row.
Enter 1 in the second column of the first row.
......
The second line ....
Column textiles are
Put 1 In the first row and 2 in the first row ,.. Put 16th rows in 16... Put 1 In the first column .. Put 2 In the first column... Put the 16th columns in 16 .. Place 1 in the first area .... Then, put the last 81 bits in your own position. Specifically, R * m + C is used.
# Include <cstdio> # include <iostream> # include <algorithm> # include <cstring> # define maxn 16*16*16*16*16*4 + 5 # define INF 0x3f3f3f3fusing namespace STD; int n = 16*16*9, M = 16*16*4, P; int map [16*16*16 + 5] [16*16*4 + 5]; int L [maxn], R [maxn], d [maxn], U [maxn], s [maxn], O [maxn], Col [maxn], row [maxn], Head, CNT; int num; void dancing_links_init () {head = 0; memset (S, 0, sizeof S); For (INT I = head; I <= m; I ++) {R [I] = (I + 1) % (m + 1); L [I] = (I-1 + m + 1) % (m + 1); U [I] = d [I] = I;} CNT = m + 1; for (INT I = 1; I <= N; I ++) {int rowh =-1; for (Int J = 1; j <= m; j ++) {If (Map [I] [J]) {s [J] ++; U [CNT] = U [J]; d [U [J] = CNT; U [J] = CNT; d [CNT] = J; row [CNT] = I; Col [CNT] = J; If (rowh =-1) {L [CNT] = R [CNT] = CNT; rowh = CNT;} else {L [CNT] = L [rowh]; R [L [rowh] = CNT; R [CNT] = rowh; L [rowh] = CNT;} CNT ++ ;}}} void remove (const Int & C) {L [R [c] = L [c]; R [L [c] = R [c]; for (INT I = d [c]; I! = C; I = d [I]) {for (Int J = R [I]; J! = I; j = R [J]) {u [d [J] = U [J]; d [U [J] = d [J]; -- s [col [J] ;}} void resume (const Int & C) {for (INT I = U [c]; I! = C; I = U [I]) {for (Int J = L [I]; J! = I; j = L [J]) {++ s [col [J]; U [d [J] = J; d [U [J] = J ;}l [R [c] = C; R [L [c] = C ;} bool DFS (const Int & K) // feasible solution {If (Head = R [head]) {sort (O, O + 256); For (INT I = 0; I <256; I ++) {printf ("% C", O [I]-I * 16 + 'a'-1 ); if (I % 16 = 15) puts ("");} puts (""); Return true;} int MX = inf, cur = 0; for (int t = R [head]; t! = Head; t = R [T]) {If (s [T] <MX) {MX = s [T]; cur = T ;}} remove (cur ); // Based on the inference conditions at the beginning, you can know that the selection is for (INT I = d [cur]; I! = Cur; I = d [I]) // select the column {// and then select which row to delete will overwrite the column's O [k] = row [I]; for (Int J = R [I]; J! = I; j = R [J]) {remove (COL [J]);} If (DFS (k + 1) return true; for (Int J = L [I]; J! = I; j = L [J]) {resume (COL [J]) ;}} resume (cur); Return false ;}/ * void DFS (const Int & K) // optimal solution {If (R [head] = head) {If (k <num) num = K; return;} If (k> = num) return; int MX = inf, cur = 0; For (int t = R [head]; t! = Head; t = R [T]) {If (s [T] <MX) {MX = s [T]; cur = T ;}} remove (cur ); for (INT I = d [cur]; I! = Cur; I = d [I]) {for (Int J = R [I]; J! = I; j = R [J]) {remove (COL [J]);} DFS (k + 1); For (Int J = L [I]; J! = I; j = L [J]) {resume (COL [J]) ;}} resume (cur) ;}*/Char TMP [16] [1111]; char STR [1111]; int main () {While (scanf ("% s", TMP [0])! = EOF) {for (INT I = 1; I <16; I ++) scanf ("% s", TMP [I]); int CNT = 0; for (INT I = 0; I <16; I ++) for (Int J = 0; j <16; j ++) STR [CNT ++] = TMP [I] [J]; memset (MAP, 0, sizeof map); int Len = CNT; For (INT I = 0; I <Len; I ++) {int r = I/16; int c = I-r * 16; int base = (R * 16 + C) * 16; if (STR [I] = '-') {for (int K = 1; k <= 16; k ++) {map [base + k] [R * 16 + k] = 1; Map [base + k] [256 + C * 16 + k] = 1; map [base + k] [256 + 256 + 16*(4 * (R/4) + (C/4) + K] = 1; map [base + k] [256*3 + R * 16 + C + 1] = 1 ;}} else {int K = STR [I]-'A' + 1; map [base + k] [R * 16 + k] = 1; Map [base + k] [256 + C * 16 + k] = 1; map [base + k] [256 + 256 + 16*(4 * (R/4) + (C/4) + K] = 1; map [base + k] [256 + 256 + 256 + R * 16 + C + 1] = 1 ;}} dancing_links_init (); DFS (0);} return 0 ;}
Poj 3076 Sudoku (dancing links)