HDU 1426 (Sudoku)

Source: Internet
Author: User

I just saw that many people use DFS + Pruning for Sudoku .. What's wrong with my DLX...

But think about the fact that this search pruning is really a lot, but it's still faster than DLX...

# Include <stdio. h> # Include < String . H> # Include <Iostream> Using   Namespace  STD;  # Define N 300000 # Define INF 0x3fffffff Char G [ 10 ] [ 10  ]; Int Ans [ 1000  ];  Int U [ 5000 ], D [ 5000 ], R [ 5000 ], L [ 5000 ], Num [ 5000 ], H [ 1000 ], Save [ 5000 ], Save1 [ 5000  ];  Int Flag, head;  Const   Int N = 729  ;  Const   Int M = 324  ;  Int  ID;  Void  Prepare (){  For ( Int I = 0 ; I <= m; I ++) {Num [I] = 0  ; D [I] = I; U [I] = I; R [I] = I + 1  ; L [I + 1 ] = I;} R [m] = 0  ; Memset (H, - 1 , Sizeof (H )); // Record the first vertex of each row  }  Void Link ( Int TN, Int  TM) {ID ++ ; Save1 [ID] = Tn; //  Record row ++ Num [save [ID] = Tm]; //  Record Column D [ID] = D [Tm]; U [d [Tm] = ID; U [ID] = TM; d [Tm] = ID;  If (H [tn] < 0 ) H [tn] = L [ID] = R [ID] = ID;  Else  {R [ID] = R [H [tn]; L [R [H [tn] = ID; R [H [tn] = ID; L [ID] = H [tn] ;}}  Void  Build () {ID = M; Int  SUM; prepare ();  Int Tn = 0  ;  For ( Int I = 1 ; I <= 81 ; I ++ ){  For ( Int J = 1 ; J <= 9 ; J ++){ ++ TN; Link (TN, I) ;}} sum = 81  ;  ///////////////  //      For ( Int I = 1 ; I <= 9 ; I ++) //  Each row  {Tn = (I- 1 )*81  ;  For ( Int K = 1 ; K <= 9 ; K ++ ){  Int TK = tn + K;  For ( Int J = 1 ; J <= 9 ; J ++ ) {Link (TK, Sum + (I- 1 )* 9 + K); TK + = 9  ; }}} Sum + = 81  ;  /////////////////////  //      For ( Int I = 1 ; I <= 9 ; I ++ ) {Tn = (I- 1 )* 9  ;  For ( Int K = 1 ; K <= 9 ; K ++ ){  Int TK = tn + K;  For ( Int J = 1 ; J <= 9 ; J ++) {Link (TK, Sum + (I- 1 )* 9 + K); TK + = 81  ; }}} Sum + = 81  ;  ////////////////////////  /      Int Tt = 0  ;  For (Int I1 = 1 ; I1 <= 3 ; I1 ++ ){  For ( Int J1 = 1 ; J1 <= 3 ; J1 ++ ) {Tn = (I1- 1 )* 81 * 3 + 9 * 3 * (J1- 1  );  For ( Int K = 1 ; K <= 9 ; K ++ ){ ++ TT;  Int  TK;  For ( Int I = 1 ; I <= 3 ; I ++ ){  For ( Int J = 1 ; J <= 3 ; J ++ ) {TK = Tn + (I- 1 )* 81 + 9 * (J- 1 ) + K; Link (TK, Sum +TT );}}}}}}  Void Remove ( Int  S) {L [R [s] = L [s]; R [L [s] = R [s];  For ( Int I = d [s]; I! = S; I = D [I])  For ( Int J = R [I]; J! = I; j = R [J]) {u [d [J] =U [J]; d [U [J] = D [J]; num [save [J] -- ;}}  Void Resume ( Int  S) {R [L [s] = S; L [R [s] = S;  For ( Int I = U [s]; I! = S; I = U [I])  For ( Int J = L [I]; J! = I; j =L [J]) {u [d [J] = J; d [U [J] = J; num [save [J] ++ ;}}  Void DFS ( Int  S ){  If (FLAG) Return  ;  If (R [head] = Head) {flag = 1  ; For ( Int I = 0 ; I <s; I ++ ){  Int  Ti, TJ, TK;  Int Tans = save1 [ans [I]- 1  ; Ti = (TANS )/ 81 + 1  ; TJ = (TANS % 81 )/ 9 +1  ; TK = (TANS % 81 ) % 9 + 1  ;  //  Printf ("<% d>", Ti, TJ ); G [Ti] [TJ] = TK + '  0  '  ;}  Return  ;}  Int Mi =INF, Tu;  For ( Int I = R [head]; I! = Head; I = R [I])  If (Mi> Num [I]) {mi = Num [I]; TU = I;} remove (TU );  For ( Int I = d [Tu]; I! = Tu; I = D [I]) {  For ( Int J = R [I]; J! = I; j = R [J]) Remove (save [J]); ans [s] = I; DFS (S + 1  );  For ( Int J = L [I]; J! = I; j = L [J]) Resume (save [J]);} resume (TU );}  Int  Main (){  Int Fflag = 0  ;  Char TT;  While (CIN> TT ){  If (Fflag = 1  ) {Printf (  "  \ N  "  );} Fflag = 1  ; Build ();  Int Tu = 0  ; For ( Int I = 1 ; I <= 9 ; I ++ ){  For ( Int J = 1 ; J <= 9 ; J ++ ){  If (I = 1 & J = 1 ) G [I] [J] = TT;  Else  CIN > G [I] [J];  If (G [I] [J]! = '  ?  '  ){  Int Kk = G [I] [J]- '  0  ' ; Remove (save [H [Tu + Kk]);  For ( Int I1 = R [H [Tu + KK]; I1! = H [Tu + KK]; I1 = R [I1]) {remove (save [I1]) ;}} TU + = 9  ;} Flag = 0  ; DFS (  0  );  For ( Int I = 1 ; I <= 9 ; I ++ ){  For ( Int J = 1 ; J < 9 ; J ++ ) Printf (  "  % C  "  , G [I] [J]); printf (  " % C  " , G [I] [ 9  ]); Printf (  "  \ N  "  );}}  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.