1 ////////////////////////////////////////////////////////////////////////////////
2 // sudokuki - C++ graphical sudoku game //
3 // Copyright (C) 2007-2009 Sylvain Vedrenne //
5 // This program is free software; you can redistribute it and/or //
6 // modify it under the terms of the GNU General Public License //
7 // as published by the Free Software Foundation; either version 2 //
8 // of the License, or (at your option) any later version. //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License for more details. //
15 // You should have received a copy of the GNU General Public License along //
16 // with this program; if not, write to the Free Software Foundation, Inc., //
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. //
18 ////////////////////////////////////////////////////////////////////////////////
20 // The source code below is the result of the initial work of Suexg's author //
21 // -congratulations to him for making Suexg's source code public domain!- //
22 // plus modifications by Sudokuki's author, for adaptation to Sudokuki. //
24 // "Suexg" is a sudoku generator written in C. "Suexg version 12" is included //
25 // in Sudokuki since version 0.0.12_gtkmm of Sudokuki. //
27 ////////////////////////////////////////////////////////////////////////////////
28 // The note below (between /* ... */) is from Suexg's author: //
29 /******************************************************************************/
30 /* suexg version 12, small randomized sudoku-generator in C. */
32 /* Generates about 24 sudokus per second with 1GHz CPU. */
33 /* Based on an exact cover solver, compiled with gcc3.2. Report bugs, */
34 /* improvement suggestions,feedback to sterten@aol.com. For some */
35 /* explanation of the solver see: http://magictour.free.fr/suexco.txt */
36 /* This generator starts from an empty grid and adds clues completely */
37 /* at random. There are faster pseudo-random methods which generate */
38 /* up to 1000 sudokus per second. [..] */
40 /* Send sudokus with rating more than 100000 to sterten@aol.com so they */
41 /* can be included in the list of hardest sudokus at */
42 /* http://magictour.free.fr/top94 [..] */
44 /* This software is public domain. */
45 /******************************************************************************/
49 #define MWC ((zr=36969*(zr&65535)+(zr>>16))^(wr=18000*(wr&65535)+(wr>>16)))
51 unsigned zr = 362436069;
52 unsigned wr = 521288629;
102 char B[83] = "0111222333111222333111222333444555666"
103 "444555666444555666777888999777888999777888999";
107 //------------------------------------------------------------
112 grid_generate ( int seed, int requestedRatingMin, int requestedRatingMax, int** grid, int* rating, int** grid_with_clues )
114 //printf("requestedRatingMin: %d\n", requestedRatingMin);
115 //printf("requestedRatingMax: %d\n", requestedRatingMax);
117 //for (k=0; k<1000; k++) {
118 // //printf("MWC : %d\n", MWC);
122 //for(ii = 0; ii<89; ii++) {
123 // //printf("B[%d] = %d\n", ii, B[ii]);
129 samples = 1; // number of grids to generate (here only one grid is generated)
130 rate = 1; // if this value is not zero, the program will calculate the rating (for each grid)
132 for ( i=0; i<888; i++ )
143 for ( x=1; x<=9; x++ )
145 for ( y=1; y<=9; y++ )
147 for ( s=1; s<=9; s++ )
150 ////printf("r : %d\n", r);
153 ////printf("Col[%d][1] : %d\n", r, Col[r][1]);
154 Col[r][2] = (B[x*9-9+y]-48)*9-9+s+81;
155 ////printf("Col[%d][2] : %d\n", r, Col[r][2]);
156 Col[r][3] = x*9-9+s+81*2;
157 ////printf("Col[%d][3] : %d\n", r, Col[r][4]);
158 Col[r][4] = y*9-9+s+81*3;
159 ////printf("Col[%d][4] : %d\n", r, Col[r][4]);
163 for ( c=1; c<=m; c++ )
167 for ( r=1; r<=n; r++ )
169 ////printf("r : %d\n", r);
170 for ( c=1; c<=Cols[r]; c++ )
173 ////printf("a : %d\n", a);
184 if ( sam1 > samples )
192 for ( i=1; i<=81; i++ )
219 //printf("\nA[i1:%d] = s:%d\n", i1, s);
222 // add a random clue and solve it. No solution ==> remove it again.
223 // Not yet a unique solution ==> continue adding clues
237 //now we have a unique-solution sudoku. Now remove clues to make it minimal
238 {//EXPERIMENTAL: here is the grid with clues in it
241 p_sol = *grid_with_clues;
243 for ( i=1; i<=81; i++ )
249 for ( i=1; i<=81; i++ )
263 for ( i1=1; i1<=81; i1++ )
276 for ( f=0; f<100; f++ )
281 ////printf ( "new grid, rating:%6i", nt );
282 if (nt < requestedRatingMin || requestedRatingMax < nt) {
293 for ( i=1; i<=81; i++ )
305 //-----------------------------------------------------------------------
306 //-----------------------------------------------------------------------
308 {//returns 0 (no solution), 1 (unique sol.), 2 (more than one sol.)
309 //printf("solve()...\n");
311 for ( i=0; i<=n; i++ )
315 for ( i=0; i<=m; i++ )
320 for ( i=1; i<=81; i++ )
324 //printf("clues:%d", clues);
327 for ( j=1; j<=Cols[r]; j++ )
335 for ( k=1; k<=Rows[d]; k++ )
343 for ( c=1; c<=m; c++ )
346 for ( r=1; r<=Rows[c]; r++ )
348 if ( Ur[Row[c][r]] == 0 )
378 for ( c=1; c<=m; c++ )
414 if ( I[i] > Rows[c] )
425 nodes++;//if(nodes>9999 && part==0)return 0;
426 for ( j=1; j <= Cols[r]; j++ )
431 for ( j=1; j<=Cols[r]; j++ )
434 for ( k=1; k<=Rows[c1]; k++ )
440 for ( l=1; l<=Cols[r1]; l++ )
444 if ( Uc[c2]+V[c2] < 1 )
448 if ( Uc[c2]==0 && V[c2]<2 )
475 for ( j=1; j<=Cols[r]; j++ )
479 for ( k=1; k<=Rows[c1]; k++ )
485 for ( l=1; l<=Cols[r1]; l++ )
500 //printf("\nsolve() => %d\n",solutions);
503 //-----------------------------------------------------------------------