int main(int argc, char** argv) { unsigned int numTests; scanf("%u\n", &numTests); unsigned int row, col; unsigned char line[17]; unsigned short sudoku[256]; while(numTests-->0) { for(row=0; row < 16; row++) { scanf("%s", line); for(col=0; col < 16; col++) switch(line[col]) { case '-': sudoku[16 * row + col] = 0xffff; break; default: setCell(sudoku, row, col, line[col] - 'A'); } } printSudoku(sudoku); solve(sudoku, -1); } return 0; }
int main() { FILE *ifp; ifp = fopen("sudoku.txt", "r"); if (ifp == NULL) { printf("Couldn't open sudoku.txt input file!\n"); return 0; } int sud[81] = {0}; int index = 0; fscanf(ifp, "%d", &sud[index]); while(!feof(ifp)) { index++; if (index > 81) { printf("sudoku.txt contains more than 81 digits! Sudoku is invalid.\n"); return 0; } fscanf(ifp, "%d", &sud[index]); } if (index < 81) { printf("sudoku.txt contains less than 81 digits! Sudoku is invalid.\n"); return 0; } fclose(ifp); sudoku(sud); printSudoku(sud); return 0; }
/* * Gibt alle Lösungen für ein Sudoku an * x,y Startwert (0,0) * Rueckgabe: 0 Versuch hat nicht funktioniert * 1 Versuch hat funktioniert */ int solve(int x, int y) { int i; if(x == G) { /* Zeilenende erreicht */ y++; x = 0; if(y == G) /* Ende erreicht */ return 1; } if(feld[y][x] > 0) /* Feld schon gesetzt */ return solve(x+1, y); /* Naechstes Feld */ for(i = 1; i <= G; i++) { /* Keine Zahl vorhanden */ if(!check(x, y, i)) { /* Alle Zahlen durchgehen */ feld[y][x] = i; /* Wenn Zahl passt, setzen */ if(solve(x+1, y)) { /* Naechstes Feld pruefen */ loesungen++; /* Loesung gefunden, ausgeben */ printf("Loesung %d:\n", loesungen); printSudoku(); printf("\n"); /*return 1;*/ /*<-- Nur eine Loesung ausgeben */ } } } feld[y][x] = 0; /* Keine Zahl hat gepasst, wieder 0 setzen */ return 0; }
int main(int argc, char** argv){ sudoku *s; while(s=readSudokuFromFile("input.txt")){ solve(s); printSudoku(s); free(s); } return 0; }
int main(){ int sudoku[9][9] = { {3, 0, 6, 5, 0, 8, 4, 0, 0}, {5, 2, 0, 0, 0, 0, 0, 0, 0}, {0, 8, 7, 0, 0, 0, 0, 3, 1}, {0, 0, 3, 0, 1, 0, 0, 8, 0}, {9, 0, 0, 8, 6, 3, 0, 0, 5}, {0, 5, 0, 0, 9, 0, 6, 0, 0}, {1, 3, 0, 0, 0, 0, 2, 5, 0}, {0, 0, 0, 0, 0, 0, 0, 7, 4}, {0, 0, 5, 2, 0, 6, 3, 0, 0}}; if (solveSudoku(sudoku) == 1) printSudoku(sudoku); else printf("No solution exists"); return 0; }
boolean solvedCellSimplifiesUnit(unsigned short* sudoku, int recursionLevel) { boolean success = false; unsigned int row, col; for(row = 0; row < 16; row++) for(col = 0; col < 16; col++) { unsigned short candidates = sudoku[16 * row + col]; if(candidates == 0) { guessConflict[recursionLevel] = true; return; } if(!isFixed(candidates)) continue; unsigned int i; for(i = 1; i < 16; i++) { // remove from row unsigned int cell = indexOfOtherRowCells(row, col, i); success |= sudoku[cell] & candidates; sudoku[cell] &= ~candidates; // remove from col cell = indexOfOtherColCells(row, col, i); success |= sudoku[cell] & candidates; sudoku[cell] &= ~candidates; // remove from box cell = indexOfOtherBoxCells(row, col, i); success |= sudoku[cell] & candidates; sudoku[cell] &= ~candidates; } } if(success && recursionLevel < 0) { printf("SolvedCellSimplifiedUnit\n"); printSudoku(sudoku); } return success; }
int main(int argc, char ** argv) { FILE * f = NULL; if (parseInput(argc, argv, &f)) { //parse our input; exit if there's an error printUsage(argv[0]); return -1; } sudoku s = initBoard(f); //initialize the board; exit if there's an error if (!s) { printUsage(argv[0]); return -1; } fclose(f); printSudoku(s, true); }
/* Input: An empty queue, and an initialized sudoku s, an empty queue * for the solutions, and a pointer to a variable to store the * number of guesses * Returns: NULL if no possible solutions exist or the queue is not * empty, and otherwise the solution to the input puzzle. * ----------------------------------------------------------- * Solves the puzzle using backtracking. The solver is initialized by * putting the puzzle into the empty queue. Then, each iteration will * pull a board out of the queue, perform a simple reduction on that * board, and then make a guess on the cell which has the least number * of possibilities. If verbose is set, it prints each board before * making a guess, giving a sense of the whole solution process. * * If there is nothing to pull out of the queue, there are no possible * solutions to the puzzle, and the function returns an error value of * NULL. If the queue is not empty at initialization, the function * prints an error message regardless of the state of the flags, and * returns an error. */ sudoku solve(queue q, sudoku s, int * guesses) { if (!isEmptyQueue(q)) { printf("Error: call to solve with a non-empty queue"); return NULL; } putQueue(q, (void *) s); *guesses = 0; int slvd = 0; while (1) { if (getQueue(q, (void **) &s)){ return NULL; } reduce(s); if(verbose) { system("clear"); printSudoku(s, pretty); printf("\n"); } slvd = checkSudoku(s); if (slvd == -1) deleteSudoku(s); else if (slvd == 1) return s; else { if(guess(q, s)) { printf("Error: Full queue"); return NULL; } deleteSudoku(s); (*guesses)++; } } }
boolean findSingles(unsigned short* sudoku, int recursionLevel) { boolean success = false; unsigned int row, col; for(row = 0; row < 16; row++) for(col = 0; col < 16; col++) { if(isFixed(sudoku[16 * row + col])) continue; /** Collect all other cells from the current unit via OR, then invert the mask and AND it with the cell candidates **/ unsigned int i; unsigned short mask = 0x0000; for(i = 1; i < 16; i++) { unsigned int cell = indexOfOtherRowCells(row, col, i); mask |= sudoku[cell]; } if(sudoku[16 * row + col] & ~mask) { success = true; sudoku[16 * row + col] &= ~mask; continue; } mask = 0x0000; for(i = 1; i < 16; i++) { unsigned int cell = indexOfOtherColCells(row, col, i); mask |= sudoku[cell]; } if(sudoku[16 * row + col] & ~mask) { success = true; sudoku[16 * row + col] &= ~mask; continue; } mask = 0x0000; for(i = 1; i < 16; i++) { unsigned int cell = indexOfOtherBoxCells(row, col, i); mask |= sudoku[cell]; } if(sudoku[16 * row + col] & ~mask) { success = true; sudoku[16 * row + col] &= ~mask; continue; } } if(success && recursionLevel < 0) { printf("findSingles\n"); printSudoku(sudoku); } return success; }
int main(int argc, char **argv){ int **board = readBoard(); printSudoku(board); printSudoku(solveSudoku(board, newSq(0,0))); }
void generate(void) { int fn, i, blockChoice; for(i = 0; i < 10; i++) { fn = rand() % 10; blockChoice = rand() % 3; switch(fn) { case 0: switch(blockChoice) { case 0: swapLittleRow(rand() % 3, rand() % 3); break; case 1: swapLittleRow((rand() % 3) + 3, (rand() % 3) + 3); break; case 2: swapLittleRow((rand() % 3) + 6, (rand() % 3) + 6); break; } break; case 1: swapBigRow((rand() % 3), (rand() % 3)); break; case 2: switch(blockChoice) { case 0: swapLittleCol((rand() % 3), (rand() % 3)); break; case 1: swapLittleCol((rand() % 3) + 3, (rand() % 3) + 3); break; case 2: swapLittleCol((rand() % 3) + 6, (rand() % 3) + 6); break; } break; case 3: swapBigCol((rand() % 3), (rand() % 3)); break; case 4: flipVert(); break; case 5: flipHorz(); break; case 6: flipDiag(); break; case 7: rotate90cw(); break; case 8: rotate90ccw(); break; case 9: replace((rand() % 9) + 1, (rand() % 9)+ 1); break; } } printSudoku(0.66); }
int main(int argc, char *argv[]) { FILE * fin = NULL; int lineCounter = 1; int sudoku[9][9] = {{0}}; int row = 0; int col = 0; int fileOK = 1; int prec, c; if( 2 != argc ) { printf("Argument error!\nUsage: %s <file-name>\n", argv[0]); return 0; } fin = fopen(argv[1], "rb"); if( NULL == fin ) { printf("Cannot open file `%s' for read!\n", argv[1]); return 1; } #define GET_NEXT_CHAR (prec = c, c = fgetc(fin)) while( fileOK && EOF != (GET_NEXT_CHAR) ) { // printf("`%c|%c',", prec, c); switch( c ) { case '#' : while( '\n' != (GET_NEXT_CHAR) ); // fall through; case '\n' : if( 0 != col%9 ) { printf("Error! Unfinished line at L%d, %d %s expected!", lineCounter, (9-col), (9-col==1? "number": "numbers") ); fileOK = 0; } // if unfinished lineCounter++; break; case '%' : if( '\n' == prec && '%' == (GET_NEXT_CHAR) ) { // line starting by `%%', treat as separator if( 0 != row%9 || 0 != col%9 ) { printf("Error!" " Incomplete sudoku before separator at L%d!", lineCounter); fileOK = 0; } // if incomplete } // if separator break; case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : sudoku[row][col++] = c-'0'; if( 9 == col ) { col = 0; row++; if( 9 == row ) { // a complete sudoku has been stored, try to solve it. printf(">> Complete sudoku, will now solve...\n"); printSudoku(sudoku); sudokuSolve(sudoku); row = 0; } // if complete } // if line finished break; default : // do nothing; break; } // switch } // while return 0; }