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;
197 return (l_int32 *)ERROR_PTR(
"filename not defined", __func__, NULL);
204 for (i = 0; i < nlines; i++) {
214 L_ERROR(
"file has %d lines\n", __func__, nlines);
215 return (l_int32 *)ERROR_PTR(
"invalid file", __func__, NULL);
221 array = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
222 for (i = 0, index = 0; i < 9; i++) {
230 for (j = 0; j < 9; j++) {
232 if (sscanf(strj,
"%d", &val) != 1)
235 array[index++] = val;
244 return (l_int32 *)ERROR_PTR(
"invalid data", __func__, NULL);
270 return (l_int32 *)ERROR_PTR(
"str not defined", __func__, NULL);
273 array = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
274 for (i = 0; i < 81; i++) {
275 if (sscanf(str + 2 * i,
"%d ", &array[i]) != 1) {
277 return (l_int32 *)ERROR_PTR(
"invalid format", __func__, NULL);
305 l_int32 i, val, locs_index;
309 return (
L_SUDOKU *)ERROR_PTR(
"array not defined", __func__, NULL);
313 sud->
locs = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
314 sud->
init = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
315 sud->
state = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
316 for (i = 0; i < 81; i++) {
321 sud->
locs[locs_index++] = i;
323 sud->
num = locs_index;
342 L_WARNING(
"ptr address is NULL\n", __func__);
345 if ((sud = *psud) == NULL)
348 LEPT_FREE(sud->
locs);
349 LEPT_FREE(sud->
init);
350 LEPT_FREE(sud->
state);
370 return ERROR_INT(
"sud not defined", __func__, 0);
373 return ERROR_INT(
"initial state not valid", __func__, 0);
411 return ERROR_INT(
"state not defined", __func__, 0);
413 for (i = 0; i < 81; i++) {
443 l_int32 index, val, valid;
444 l_int32 *locs, *state;
486 l_int32 i, j, val, row, rowstart, rowend, col;
487 l_int32 blockrow, blockcol, blockstart, rowindex, locindex;
489 if ((val = state[index]) == 0)
495 for (i = rowstart; i < index; i++) {
499 rowend = rowstart + 9;
500 for (i = index + 1; i < rowend; i++) {
507 for (j = col; j < index; j += 9) {
511 for (j = index + 9; j < 81; j += 9) {
517 blockrow = 3 * (row / 3);
518 blockcol = 3 * (col / 3);
519 blockstart = 9 * blockrow + blockcol;
520 for (i = 0; i < 3; i++) {
521 rowindex = blockstart + 9 * i;
522 for (j = 0; j < 3; j++) {
523 locindex = rowindex + j;
524 if (index == locindex)
continue;
525 if (state[locindex] == val)
557 l_int32 same1, same2, same3;
558 l_int32 *array1, *array2, *array3;
562 return ERROR_INT(
"&unique not defined", __func__, 1);
565 return ERROR_INT(
"array not defined", __func__, 1);
582 *punique = (same1 && same2 && same3);
622 return ERROR_INT(
"&same not defined", __func__, 1);
625 return ERROR_INT(
"sud1 not defined", __func__, 1);
627 return ERROR_INT(
"sud1 not defined", __func__, 1);
628 if (quads < 1 || quads > 3)
629 return ERROR_INT(
"valid quads in {1,2,3}", __func__, 1);
633 return ERROR_INT(
"array not made", __func__, 1);
634 for (i = 0; i < 81; i++) {
635 if (array[i] != sud2->
state[i]) {
657 l_int32 i, j, sindex, dindex;
661 return (l_int32 *)ERROR_PTR(
"array not defined", __func__, NULL);
662 if (quads < 1 || quads > 3)
663 return (l_int32 *)ERROR_PTR(
"valid quads in {1,2,3}", __func__, NULL);
665 rarray = (l_int32 *)LEPT_CALLOC(81,
sizeof(l_int32));
667 for (j = 0, dindex = 0; j < 9; j++) {
668 for (i = 8; i >= 0; i--) {
670 rarray[dindex++] = array[sindex];
673 }
else if (quads == 2) {
674 for (i = 8, dindex = 0; i >= 0; i--) {
675 for (j = 8; j >= 0; j--) {
677 rarray[dindex++] = array[sindex];
681 for (j = 8, dindex = 0; j >= 0; j--) {
682 for (i = 0; i < 9; i++) {
684 rarray[dindex++] = array[sindex];
722 l_int32 index, sector, nzeros, removefirst, tries, val, oldval, unique;
726 return (
L_SUDOKU *)ERROR_PTR(
"array not defined", __func__, NULL);
728 return (
L_SUDOKU *)ERROR_PTR(
"minelems must be < 81", __func__, NULL);
737 removefirst = L_MIN(30, 81 - minelems);
738 while (nzeros < removefirst) {
740 index = 27 * (sector / 3) + 3 * (sector % 3) +
741 9 * (val / 3) + (val % 3);
742 if (array[index] == 0)
continue;
752 L_ERROR(
"invalid initial solution\n", __func__);
758 L_ERROR(
"non-unique result with 30 zeroes\n", __func__);
766 if (tries > maxtries)
break;
767 if (81 - nzeros <= minelems)
break;
777 index = 27 * (sector / 3) + 3 * (sector % 3) +
778 9 * (val / 3) + (val % 3);
781 if (array[index] == 0)
continue;
784 oldval = array[index];
790 if (testsud->
failure == TRUE) {
792 array[index] = oldval;
801 array[index] = oldval;
845 return ERROR_INT(
"sud not defined", __func__, 1);
846 if (arraytype == L_SUDOKU_INIT)
848 else if (arraytype == L_SUDOKU_STATE)
851 return ERROR_INT(
"invalid arraytype", __func__, 1);
853 for (i = 0; i < 9; i++) {
854 for (j = 0; j < 9; j++)
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
SARRAY * sarrayCreateWordsFromString(const char *string)
sarrayCreateWordsFromString()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
l_int32 * sudokuReadFile(const char *filename)
sudokuReadFile()
static l_int32 sudokuNewGuess(L_SUDOKU *sud)
sudokuNewGuess()
static l_int32 sudokuValidState(l_int32 *state)
sudokuValidState()
l_int32 * sudokuReadString(const char *str)
sudokuReadString()
L_SUDOKU * sudokuGenerate(l_int32 *array, l_int32 seed, l_int32 minelems, l_int32 maxtries)
sudokuGenerate()
static l_int32 * sudokuRotateArray(l_int32 *array, l_int32 quads)
sudokuRotateArray()
static l_int32 sudokuCompareState(L_SUDOKU *sud1, L_SUDOKU *sud2, l_int32 quads, l_int32 *psame)
sudokuCompareState()
void sudokuDestroy(L_SUDOKU **psud)
sudokuDestroy()
L_SUDOKU * sudokuCreate(l_int32 *array)
sudokuCreate()
static l_int32 sudokuTestState(l_int32 *state, l_int32 index)
sudokuTestState()
l_int32 sudokuOutput(L_SUDOKU *sud, l_int32 arraytype)
sudokuOutput()
l_ok sudokuTestUniqueness(l_int32 *array, l_int32 *punique)
sudokuTestUniqueness()
l_int32 sudokuSolve(L_SUDOKU *sud)
sudokuSolve()
l_ok genRandomIntOnInterval(l_int32 start, l_int32 end, l_int32 seed, l_int32 *pval)
genRandomIntOnInterval()
void lept_stderr(const char *fmt,...)
lept_stderr()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()