191l_int32 i, j, nlines, val, index, error;
194SARRAY *saline, *sa1, *sa2;
197 return (l_int32 *)ERROR_PTR(
"filename not defined", __func__, NULL);
198 data = l_binaryRead(filename, &size);
199 sa1 = sarrayCreateLinesFromString((
char *)data, 0);
200 sa2 = sarrayCreate(9);
203 nlines = sarrayGetCount(sa1);
204 for (i = 0; i < nlines; i++) {
205 str = sarrayGetString(sa1, i,
L_NOCOPY);
207 sarrayAddString(sa2, str,
L_COPY);
211 nlines = sarrayGetCount(sa2);
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++) {
223 str = sarrayGetString(sa2, i,
L_NOCOPY);
224 saline = sarrayCreateWordsFromString(str);
225 if (sarrayGetCount(saline) != 9) {
227 sarrayDestroy(&saline);
230 for (j = 0; j < 9; j++) {
231 strj = sarrayGetString(saline, j,
L_NOCOPY);
232 if (sscanf(strj,
"%d", &val) != 1)
235 array[index++] = val;
237 sarrayDestroy(&saline);
244 return (l_int32 *)ERROR_PTR(
"invalid data", __func__, NULL);
305l_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;
486l_int32 i, j, val, row, rowstart, rowend, col;
487l_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)
657l_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];
722l_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) {
739 genRandomIntOnInterval(0, 8, 0, &val);
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;
770 lept_stderr(
"Trying %d zeros\n", nzeros);
776 genRandomIntOnInterval(0, 8, 0, &val);
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;
805 lept_stderr(
"Have %d zeros\n", nzeros);
809 lept_stderr(
"Final: nelems = %d\n", 81 - nzeros);