Sudoku is a time-consuming and brain-consuming game. Generally, it takes about one hour for a difficult Sudoku to play. I am a pseudo-Sudoku enthusiast and it takes several hours to reach a difficult Sudoku, as a result, I was too lazy to write a program to crack the attack. I hope someone will like it.
Ideas:
1. Start from the first space, calculate all the possible numbers, take out the first one for filling, and record the remaining possible numbers;
2. Calculate all possible numbers in the next space (based on the padding on the front side), fill the first possible number, and fill the remaining records;
3. Continue with this operation;
4. Until such a space is displayed: All numbers cannot be filled (conflict exists), the last hypothetical point is returned, and other possible numbers are retried;
5. Continue with this;
6. Wait until all spaces are filled.
Implementation Code:
Using system; using system. collections. generic; using system. text; /* =================================================== * author: zhu B * blog: http://blog.csdn.net/sq_zhuyi * Email: sqzhuyi@gmail.com =================================================== = */namespace sudokuresponder {public class sudokuservice {number [] [] [] [] Parr = NULL; // store the public sudokuservice (string numstr) {string S = numstr. replace (",", ""); If (S. leng Th! = 81) {Throw new exception ("Initial Sudoku error, must be 81 digits");} int [] numarr = new int [81]; for (INT I = 0; I <S. length; I ++) {numarr [I] = convert. toint32 (s [I]. tostring ();} Parr = getarray (numarr);} public sudokuservice (INT [] numarr) {If (numarr. length! = 81) {Throw new exception ("Initial Sudoku error, must be 81 digits");} Parr = getarray (numarr );} /// <summary> /// obtain the result /// </Summary> /// <returns> </returns> Public String getresult () {fillarray (); stringbuilder sb = new stringbuilder (); For (INT m = 0; m <3; m ++) {for (INT I = 0; I <3; I ++) {for (INT n = 0; n <3; n ++) {for (Int J = 0; j <3; j ++) {sb. append (Parr [m] [N] [I] [J]. value) ;}} sb. append (",") ;}} re Turn sb. tostring (). trimend (',');} // <summary> // print the result // </Summary> Public void printresult () {for (INT m = 0; m <3; m ++) {If (M = 0) console. writeline ("". padleft (25, '-'); For (INT I = 0; I <3; I ++) {for (INT n = 0; n <3; N ++) {If (n = 0) console. write ("|"); For (Int J = 0; j <3; j ++) {var Pv = Parr [m] [N] [I] [J]; if (Pv. default) console. foregroundcolor = consolecolor. red; Console. Write ("{0}", PV. value); console. foregroundcolor = consolecolor. white;} console. write ("|");} console. writeline ();} console. writeline ("". padleft (25, '-');} private number [] [] [] [] getarray (INT [] defval) {number [] [] [] [] Parr = new number [3] [] [] []; for (INT m = 0; m <3; m ++) {Parr [m] = new number [3] [] []; for (INT n = 0; n <3; n ++) {Parr [m] [N] = new number [3] []; for (INT I = 0; I <3; I ++) {Parr [m] [N] [I] = new number [3]; for (Int J = 0; j <3; j ++) {Parr [m] [N] [I] [J] = new number () {value = 0, default = false };}} int C = 0; for (INT m = 0; m <3; m ++) {for (INT I = 0; I <3; I ++) {for (INT n = 0; n <3; n ++) {for (Int J = 0; j <3; j ++) {int val = defval [C ++]; parr [m] [N] [I] [J]. value = val; If (Val> 0) Parr [m] [N] [I] [J]. default = true ;}}} return Parr;} private Vo Id fillarray () {for (INT m = 0; m <3; m ++) {for (INT n = 0; n <3; n ++) {for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {If (Parr [m] [N] [I] [J]. default = false) {// empty cell var Pv = getnumber (New int [4] {m, n, I, j}); If (! PV. OK) {M = PV. position [0]; n = PV. position [1]; I = PV. position [2]; j = PV. position [3]; clearafter (Pv. position);} Parr [m] [N] [I] [J]. value = PV. value ;}}// end small }// end big} // clear the space behind the position private void clearafter (INT [] POS) {int min = POS [0] * 1000 + POS [1] * 100 + POS [2] * 10 + POS [3]; for (INT m = 0; m <3; m ++) {for (INT n = 0; n <3; n ++) {for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {If (M * 1000 + N * 100 + I * 10 + j> min) {If (Parr [m] [N] [I] [J]. default = false) {Parr [m] [N] [I] [J]. value = 0 ;}}}}}// records possible numbers in each position. Private list <posvalues> posvals = new list <posvalues> (); // obtain the possible number private posvalues getnumber (INT [] POS) {list <int> Nums = new list <int> (); For (INT n = 1; n <= 9; n ++) {nums. add (n) ;}// 3 gangular for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {int A = Parr [POS [0] [POS [1] [I] [J]. value; if (a> 0 & nums. contains (a) {nums. remove (a) ;}}// lateral for (INT n = 0; n <3; n ++) {for (Int J = 0; j <3; j ++) {int A = Parr [POS [0] [N] [POS [2] [J]. value; if (a> 0 & nums. contains (a) {nums. remove (a) ;}}// vertical for (INT m = 0; m <3; m ++) {for (INT I = 0; I <3; I ++) {int A = Parr [m] [POS [1] [I] [POS [3]. value; if (a> 0 & nums. contains (a) {nums. remove (a) ;}} if (nums. count = 0) {If (posvals. count = 0) {return New posvalues () {value = 0};} var Pv = posvals [posvals. count-1]; PV. OK = false; PV. value = PV. values [0]; PV. values. remove (Pv. value); If (Pv. values. count = 0) {posvals. remove (PV) ;}return PV ;}else {var Pv = new posvalues (); PV. position = Pos; PV. value = Nums [0]; nums. remove (Pv. value); PV. values = Nums; If (nums. count> 0) {posvals. add (PV) ;}return PV ;}} public struct number {public int value; Public bool default;} public class posvalues {public int [] position; public list <int> values; Public bool OK = true; Public int value ;}}
Call code:
Using system; using system. collections. generic; namespace sudokuresponder {class program {static void main (string [] ARGs) {string S = "000000007,008000400, 0000001600,8000006201, 000000000,105407908, limit, 400000005 "; sudokuservice service = new sudokuservice (s); datetime start = datetime. now; string result = service. getresult (); timespan span = datetime. now-start; service. printresult (); console. writeline ("computing ended in {0} milliseconds", span. milliseconds); console. writeline ("press enter to exit"); console. readline ();}}}
Run:
: Http://download.csdn.net/detail/sq_zhuyi/4411715