Use DLX in klsudoku to quickly describe data independence code

Source: Internet
Author: User

This part of the code is transplanted from another c ++ program, and the connection to the original C ++ Code cannot be found at the moment, however, it is not a problem to push the C ++ code back from the C # code. At that time, porting this code didn't waste any effort.

Using DLX (dancing link X) to solve the exact cover (precise coverage) Problem of Data independence is a very common method. There are many similar codes. However, a considerable number of people are trying and backtracking to solve the problem.

Currently, the computing efficiency of computers is very high, and the 9x9 matrix of DT alone does not have much data. Therefore, from the perspective of people, we cannot feel the speed difference between the two solutions.

However, in the process of generating a Sudoku question, it takes a lot of times to Solve the Problem repeatedly. Therefore, the faster the problem is, the better.

I posted this part of the code in klsudoku here separately, and the complete code can be viewed from the svn tab on the project home page: http://code.google.com/p/klsudoku

(It's strange that you can't insert code, so you have to paste it directly)

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. diagnostics;
Using system. collections;
Using system. IO;
Using system. xml;
/*
* Project home: http://code.google.com/p/klsudoku
* Project owner: ttylikl@gmail.com E-mail: ttylikl@qq.com
* Project blog: http://www.cnblogs.com/ttylikl
* All files of this project is free to use, but you should keep the information above when you copy & use it.
* Infomation modify Date: 2009.02.22
*
**/
Namespace sudokupanel
{
Public class dlx_solver
{
Const int RR = 729;
Const int cc = 324;
Constint INF = 1000000000;
Int [] mem = new int [RR + 9];
Int [] ans = new int [RR + 9];
Char [] CH = new char [RR + 9];
Int [] CNT = new int [CC + 9];
Class Node
{
Public int R, C;
Public node up;
Public node down;
Public node left;
Public node right;
};
Node head;
Node [] All = new node [RR * CC + 99];
Node [] ROW = new node [RR];
Node [] col = new node [CC];
Int all_t;
Void Link (int r, int C)
{
CNT [c] ++;
Node T = All [all_t];
If (t = NULL)
{
T = new node ();
All [all_t] = T;
}
All_t ++;
T. R = R;
T. C = C;
T. Left = row [R];
T. Right = row [R]. Right;
T. Left. Right = T;
T. Right. Left = T;
T. Up = Col [c];
T. Down = Col [C]. down;
T. up. Down = T;
T. down. Up = T;
}
Void remove (int c)
{
Node T, TT;
Col [C]. Right. Left = Col [C]. Left;
Col [C]. Left. Right = Col [C]. Right;
For (t = Col [C]. Down ;! Object. referenceequals (T, Col [c]); t = T. down)
{
For (TT = T. Right ;! Object. referenceequals (TT, T); TT = TT. Right)
{
CNT [TT. C] --;
TT. up. Down = TT. down;
TT. down. Up = TT. up;
}
T. Left. Right = T. Right;
T. Right. Left = T. Left;
}
}
Void resume (int c)
{
Node T, TT;
For (t = Col [C]. Down ;! Object. referenceequals (T, Col [c]); t = T. down)
{
T. Right. Left = T;
T. Left. Right = T;
For (TT = T. Left ;! Object. referenceequals (TT, T); TT = TT. Left)
{
CNT [TT. C] ++;
TT. down. Up = tt;
TT. up. Down = tt;
}
}
Col [C]. Left. Right = Col [c];
Col [C]. Right. Left = Col [c];
}
Int solve (int K)
{
If (object. referenceequals (head. Right, head ))
Return 1;
Node T, TT;
Int min = inf, Tc = 0;
For (t = head. Right ;! Object. referenceequals (T, head); t = T. Right)
{
If (CNT [T. C] <min)
{
Min = CNT [T. C];
TC = T. C;
If (Min <= 1) break;
}
}
Remove (TC );
Int SCNT = 0;
For (t = Col [TC]. Down ;! Object. referenceequals (T, Col [TC]); t = T. down)
{
// If (MEM [k]! = 0)
// Debug. Write ("");
Mem [k] = T. R;
T. Left. Right = T;
For (TT = T. Right ;! Object. referenceequals (TT, T); TT = TT. Right)
{
Remove (TT. C );
}
T. Left. Right = T. Right;
SCNT + = solve (k + 1 );
If (! Chk_unique & SCNT = 1)
Return SCNT;
If (SCNT> 1)
Return SCNT;
// Continue to find the next possible
T. Right. Left = T;
For (TT = T. Left ;! Object. referenceequals (TT, T); TT = TT. Left)
{
Resume (TT. C );
}
T. Right. Left = T. Left;
}

Resume (TC );
Return SCNT;
}

Private bool chk_unique = false;
Public int scount = 0;
Public int solution_count (string Str)
{
Chk_unique = true;
Run (STR );
Return scount;
}
Public String do_solve (string Str)
{
Chk_unique = false;
String ret = run (STR );
If (scount! = 1)
Return "";
Return ret;
}
Private string run (string Str)
{
Mem = new int [RR + 9];
Ans = new int [RR + 9];
Ch = new char [RR + 9];

// Debug. writeline ("dlx_solve (" + STR + ")");
String S = Str. Replace ("\ r ","");
S = S. Replace ("\ n ","");
Ch = S. tochararray ();

CNT = new int [CC + 9];
Head = new node ();
All = new node [RR * CC + 99];
Row = new node [RR];
Col = new node [CC];
/* While (gets (CH ))*/
{
Int I;
// If (CH [0] = 'E') break;
All_t = 1;
CNT = new int [CC + 9];
Head. Left = head;
Head. Right = head;
Head. Up = head;
Head. Down = head;
Head. r = RR;
Head. c = cc;
For (I = 0; I <cc; I ++)
{
Col [I] = new node ();
Col [I]. c = I;
Col [I]. r = RR;
Col [I]. Up = Col [I];
Col [I]. Down = Col [I];
Col [I]. Left = head;
Col [I]. Right = head. Right;
Col [I]. Left. Right = Col [I];
Col [I]. Right. Left = Col [I];
}
For (I = 0; I <RR; I ++)
{
Row [I] = new node ();
Row [I]. r = I;
Row [I]. c = cc;
Row [I]. Left = row [I];
Row [I]. Right = row [I];
Row [I]. Up = head;
Row [I]. Down = head. down;
Row [I]. up. Down = row [I];
Row [I]. down. Up = row [I];
}
For (I = 0; I <RR; I ++)
{
Int r = I/9/9% 9;
Int c = I/9% 9;
Int val = I % 9 + 1;
If (CH [R * 9 + C] = '. '| ch [R * 9 + C] = '0' | ch [R * 9 + C] = Val + '0 ')
{
Link (I, R * 9 + val-1 );
Link (I, 81 + C * 9 + val-1 );
Int TR = R/3;
Int Tc = C/3;
Link (I, 162 + (tr * 3 + Tc) * 9 + val-1 );
Link (I, 243 + R * 9 + C );
}
}
For (I = 0; I <RR; I ++)
{
Row [I]. Left. Right = row [I]. Right;
Row [I]. Right. Left = row [I]. Left;
}
Scount = solve (1 );
For (I = 1; I <= 81; I ++)
{
Int T = mem [I]/9% 81;
Int val = mem [I] % 9 + 1;
// Debug. writeline ("t =" + T. tostring ("00") + "mem [" + I. tostring ("D2") + "] =" + mem [I] + "Val =" + Val + "I =" + I. tostring ("D2 "));
Ans [T] = val;
}
Stringbuilder sb = new stringbuilder ();
For (I = 0; I <81; I ++)
{
// Debug. Write (ANS [I]);
// If (ANS [I] = 0)
// Debug. Write ("Val = 0! ");
SB. append (ANS [I]);
}
// Debug. writeline ("");
// Debug. writeline ("SCNT =" + solution_count );
Return sb. tostring ();
}
}
}
}

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.