142 #include <config_auto.h> 145 #include "allheaders.h" 151 l_int32 quads, l_int32 *psame);
191 l_int32 i, j, nlines, val, index, error;
194 SARRAY *saline, *sa1, *sa2;
196 PROCNAME(
"sudokuReadFile");
199 return (l_int32 *)ERROR_PTR(
"filename not defined", procName, NULL);
206 for (i = 0; i < nlines; i++) {
216 L_ERROR(
"file has %d lines\n", procName, nlines);
217 return (l_int32 *)ERROR_PTR(
"invalid file", procName, NULL);
223 array = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
224 for (i = 0, index = 0; i < 9; i++) {
232 for (j = 0; j < 9; j++) {
234 if (sscanf(strj,
"%d", &val) != 1)
237 array[index++] = val;
246 return (l_int32 *)ERROR_PTR(
"invalid data", procName, NULL);
271 PROCNAME(
"sudokuReadString");
274 return (l_int32 *)ERROR_PTR(
"str not defined", procName, NULL);
277 array = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
278 for (i = 0; i < 81; i++) {
279 if (sscanf(str + 2 * i,
"%d ", &array[i]) != 1) {
281 return (l_int32 *)ERROR_PTR(
"invalid format", procName, NULL);
309 l_int32 i, val, locs_index;
312 PROCNAME(
"sudokuCreate");
315 return (
L_SUDOKU *)ERROR_PTR(
"array not defined", procName, NULL);
319 sud->
locs = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
320 sud->
init = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
321 sud->
state = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
322 for (i = 0; i < 81; i++) {
327 sud->
locs[locs_index++] = i;
329 sud->
num = locs_index;
347 PROCNAME(
"sudokuDestroy");
350 L_WARNING(
"ptr address is NULL\n", procName);
353 if ((sud = *psud) == NULL)
356 LEPT_FREE(sud->
locs);
357 LEPT_FREE(sud->
init);
358 LEPT_FREE(sud->
state);
377 PROCNAME(
"sudokuSolve");
380 return ERROR_INT(
"sud not defined", procName, 0);
383 return ERROR_INT(
"initial state not valid", procName, 0);
420 PROCNAME(
"sudokuValidState");
423 return ERROR_INT(
"state not defined", procName, 0);
425 for (i = 0; i < 81; i++) {
455 l_int32 index, val, valid;
456 l_int32 *locs, *state;
498 l_int32 i, j, val, row, rowstart, rowend, col;
499 l_int32 blockrow, blockcol, blockstart, rowindex, locindex;
501 if ((val = state[index]) == 0)
507 for (i = rowstart; i < index; i++) {
511 rowend = rowstart + 9;
512 for (i = index + 1; i < rowend; i++) {
519 for (j = col; j < index; j += 9) {
523 for (j = index + 9; j < 81; j += 9) {
529 blockrow = 3 * (row / 3);
530 blockcol = 3 * (col / 3);
531 blockstart = 9 * blockrow + blockcol;
532 for (i = 0; i < 3; i++) {
533 rowindex = blockstart + 9 * i;
534 for (j = 0; j < 3; j++) {
535 locindex = rowindex + j;
536 if (index == locindex)
continue;
537 if (state[locindex] == val)
569 l_int32 same1, same2, same3;
570 l_int32 *array1, *array2, *array3;
573 PROCNAME(
"sudokuTestUniqueness");
576 return ERROR_INT(
"&unique not defined", procName, 1);
579 return ERROR_INT(
"array not defined", procName, 1);
596 *punique = (same1 && same2 && same3);
635 PROCNAME(
"sudokuCompareState");
638 return ERROR_INT(
"&same not defined", procName, 1);
641 return ERROR_INT(
"sud1 not defined", procName, 1);
643 return ERROR_INT(
"sud1 not defined", procName, 1);
644 if (quads < 1 || quads > 3)
645 return ERROR_INT(
"valid quads in {1,2,3}", procName, 1);
649 return ERROR_INT(
"array not made", procName, 1);
650 for (i = 0; i < 81; i++) {
651 if (array[i] != sud2->
state[i]) {
673 l_int32 i, j, sindex, dindex;
676 PROCNAME(
"sudokuRotateArray");
679 return (l_int32 *)ERROR_PTR(
"array not defined", procName, NULL);
680 if (quads < 1 || quads > 3)
681 return (l_int32 *)ERROR_PTR(
"valid quads in {1,2,3}", procName, NULL);
683 rarray = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
685 for (j = 0, dindex = 0; j < 9; j++) {
686 for (i = 8; i >= 0; i--) {
688 rarray[dindex++] = array[sindex];
691 }
else if (quads == 2) {
692 for (i = 8, dindex = 0; i >= 0; i--) {
693 for (j = 8; j >= 0; j--) {
695 rarray[dindex++] = array[sindex];
699 for (j = 8, dindex = 0; j >= 0; j--) {
700 for (i = 0; i < 9; i++) {
702 rarray[dindex++] = array[sindex];
740 l_int32 index, sector, nzeros, removefirst, tries, val, oldval, unique;
743 PROCNAME(
"sudokuGenerate");
746 return (
L_SUDOKU *)ERROR_PTR(
"array not defined", procName, NULL);
748 return (
L_SUDOKU *)ERROR_PTR(
"minelems must be < 81", procName, NULL);
757 removefirst = L_MIN(30, 81 - minelems);
758 while (nzeros < removefirst) {
760 index = 27 * (sector / 3) + 3 * (sector % 3) +
761 9 * (val / 3) + (val % 3);
762 if (array[index] == 0)
continue;
772 L_ERROR(
"invalid initial solution\n", procName);
778 L_ERROR(
"non-unique result with 30 zeroes\n", procName);
786 if (tries > maxtries)
break;
787 if (81 - nzeros <= minelems)
break;
797 index = 27 * (sector / 3) + 3 * (sector % 3) +
798 9 * (val / 3) + (val % 3);
801 if (array[index] == 0)
continue;
804 oldval = array[index];
810 if (testsud->
failure == TRUE) {
812 array[index] = oldval;
821 array[index] = oldval;
864 PROCNAME(
"sudokuOutput");
867 return ERROR_INT(
"sud not defined", procName, 1);
868 if (arraytype == L_SUDOKU_INIT)
870 else if (arraytype == L_SUDOKU_STATE)
873 return ERROR_INT(
"invalid arraytype", procName, 1);
875 for (i = 0; i < 9; i++) {
876 for (j = 0; j < 9; j++)
static l_int32 sudokuCompareState(L_SUDOKU *sud1, L_SUDOKU *sud2, l_int32 quads, l_int32 *psame)
sudokuCompareState()
l_ok genRandomIntOnInterval(l_int32 start, l_int32 end, l_int32 seed, l_int32 *pval)
genRandomIntOnInterval()
static l_int32 sudokuNewGuess(L_SUDOKU *sud)
sudokuNewGuess()
void lept_stderr(const char *fmt,...)
lept_stderr()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
L_SUDOKU * sudokuGenerate(l_int32 *array, l_int32 seed, l_int32 minelems, l_int32 maxtries)
sudokuGenerate()
l_int32 sudokuSolve(L_SUDOKU *sud)
sudokuSolve()
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
l_ok sudokuTestUniqueness(l_int32 *array, l_int32 *punique)
sudokuTestUniqueness()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
static l_int32 sudokuValidState(l_int32 *state)
sudokuValidState()
static l_int32 * sudokuRotateArray(l_int32 *array, l_int32 quads)
sudokuRotateArray()
l_int32 * sudokuReadFile(const char *filename)
sudokuReadFile()
l_int32 sudokuOutput(L_SUDOKU *sud, l_int32 arraytype)
sudokuOutput()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
static l_int32 sudokuTestState(l_int32 *state, l_int32 index)
sudokuTestState()
l_int32 * sudokuReadString(const char *str)
sudokuReadString()
void sudokuDestroy(L_SUDOKU **psud)
sudokuDestroy()
L_SUDOKU * sudokuCreate(l_int32 *array)
sudokuCreate()
SARRAY * sarrayCreateWordsFromString(const char *string)
sarrayCreateWordsFromString()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()