void tryit(Puzzle *puzzle, PuzzleCreater create, int showpuzzle, int showroot) { Table fixes; puzzle->lastmove = 0; puzzle->lastmovecount = 0; fillTable(fixes, UNFIXED); fillTableRow(fixes, 0, FIXED); fillTableRow(fixes, TABLE_Y_SIZE - 1, FIXED); fillTabelColumn(fixes, 0, FIXED); fillTabelColumn(fixes, TABLE_X_SIZE - 1, FIXED); puzzle->show = 0; create(puzzle); if (showpuzzle != 0) { printPuzzle(puzzle); } puzzle->count = 0; puzzle->show = showroot; solve(puzzle, fixes); if (showpuzzle != 0) { printBar(); printPuzzle(puzzle); printf("count: %d\n", puzzle->count); } }
int solve(Puzzle *puzzle, Table fixes) { Table nextmap; Position pos; int i, j, m, n = 1; for (i = 1; i <= Y_SIZE - 2; i++, n++) { dcode(printf("i=%d\n", i)); for (j = 1; j <= X_SIZE - 2; j++, n++) { pos = number2postion(n); if (moveNumber(puzzle, fixes, n, pos) < 0) { printPuzzle(puzzle); printf("error j=%d\n", n); return -1; } fixes[pos.y][pos.x] = FIXED; } moveRight2Numbers(puzzle, fixes, j, i, n++); } for (j = 1; j <= X_SIZE - 2; j++, n++) { moveLeft2Numbers(puzzle, fixes, j, i, n); } moveNumber(puzzle, fixes, n, number2postion(n)); n++; moveNumber(puzzle, fixes, n, number2postion(n)); pos = makePosition(X_SIZE, Y_SIZE); searchSpaceRoot(puzzle, fixes, nextmap, pos); moveSpace(puzzle, nextmap, pos); return 0; }
void interfaceLoop() { Puzzle *puzzle = NULL; char command = '\0'; showSolution = false; createDirectionKeys(); createOptionKeys(); do { puzzle = createPuzzle(getPuzzleSize()); shuffleCount = getShuffleCount(); shufflePuzzle(puzzle, shuffleCount); showSolution = false; while( !isSolved(puzzle) ){ printHeader(); printPuzzle(puzzle); command = getNextCommand(puzzle); if(command == INVERT_KEY) { puzzle->inverted = (puzzle->inverted) ? false : true; invertSolution(puzzle); setDirectionVectors(puzzle); } else if(command == SOLVE_KEY) showSolution = (showSolution) ? false : true; else if(command == QUIT_KEY || command == RESTART_KEY) break; else applyDirection(puzzle, getData(directionKeys, command)); } if( isSolved(puzzle) ) { printPuzzle(puzzle); printWinner(); } destroyPuzzle(&puzzle); } while(command == RESTART_KEY); destroyOptionKeys(); destroyDirectionKeys(); }
void moveOneSpace(Puzzle *puzzle, const int dx, const int dy) { int x = (puzzle->space.x += dx); int y = (puzzle->space.y += dy); int n = (puzzle->table[y - dy][x - dx] = puzzle->table[y][x]); puzzle->table[y][x] = 0; puzzle->count++; if (puzzle->show != 0) { dcode(printBar()); printf("%d\n", n); dcode(printPuzzle(puzzle)); if (puzzle->lastmovecount > 4) { printf("lastmove error %d\n", n); printPuzzle(puzzle); exit(EXIT_SUCCESS); } if (puzzle->lastmove == n) { puzzle->lastmovecount++; } else { puzzle->lastmovecount = 0; puzzle->lastmove = n; } } }
int main(){ int i = 1; //randomPuzzle(); while(1){ if(findSolution('0',0,i++))break; } i=0; while(sol[i] != '\0') printf("%c",sol[i++]); printf("\n"); printPuzzle(); return 0; }
int main() { printf("Project Euler - Problem 96:\n" "By solving fifty Sudoku puzzles, find the sum of the 3-digit numbers found in the top left corner of each solution grid.\n\n"); puzzle sudoku[50]; // Struct for all 50 Sudoku grids and candidates bool verbose = false; // Read in game grids nameFile = fopen ("problem_096.txt", "rt"); int grid = -1, row = -1, col = 0, ch; bool skip = false; while((ch = fgetc(nameFile))) { if (ch == EOF) break; if (ch == 'G') { skip = true; row = -1; grid++; } if (ch == '\n') { row++; col = 0; skip = false; continue; } if (skip == true) continue; ch -= '0'; // Convert from ASCII to DEC sudoku[grid].cell[row][col][0] = ch; col++; } fclose(nameFile); // Solve grid by grid int numSolved = 0; for (int i=0; i<50; i++) { if (verbose == true) printf("Grid: %d\n", i+1); if (verbose == true) printPuzzle(sudoku[i]); // Simple deduction first solveGrid(&sudoku[i]); if (isSolved(sudoku[i])) { numSolved++; if (verbose == true) printPuzzle(sudoku[i]); continue; } // Cycle through options looking for answers guessSolution(&sudoku[i], 1); if (isSolved(sudoku[i])) { numSolved++; if (verbose == true) printPuzzle(sudoku[i]); continue; } if (verbose == true) printf("Unsolved...\n"); if (verbose == true) printPuzzle(sudoku[i]); } // Add up top, left-most numbers int sum = 0; for (int i=0; i<50; i++) { int num = sudoku[i].cell[0][0][0]*100; num += sudoku[i].cell[0][1][0] * 10; num += sudoku[i].cell[0][2][0]; sum += num; if (verbose == true) printf("Grid %2d: %d\n", i+1, num); } printf("Solved %d out of %d.\n", numSolved, 50); printf("Sum: %d\n", sum); return 0; }
void moveLeft2Numbers(Puzzle *puzzle, Table fixes, const int x, const int y, int n) { int m = n; Position pos1 = makePosition(x, y); Position pos2 = makePosition(x, y + 1); Position pos3 = makePosition(x + 1, y); Position pos4 = makePosition(x + 1, y + 1); n += X_SIZE; dcode(printf("moveLeft2Numbers x=%d y=%d n=%d\n", x, y, m)); for (;;) { Table nextmap; int p1 = puzzle->table[y][x]; int p2 = puzzle->table[y + 1][x]; int p3 = puzzle->table[y][x + 1]; int p4 = puzzle->table[y + 1][x + 1]; dputs("loop head"); if (p1 == m && p2 == n) { dputs("OK"); break; } else if (p1 != m && p1 != n && p2 != m && p2 != n) { Table tempM, tempN; Position posM = searchNumber(puzzle, m); Position posN = searchNumber(puzzle, n); int rM = searchRoot(fixes, tempM, posM, pos2); int rN = searchRoot(fixes, tempN, posN, pos1); dputs("not found case"); if (rM >= 0 && rM < rN) { if (moveNumber(puzzle, fixes, m, pos2) < 0) { printPuzzle(puzzle); puts("error"); return; } } else if (rN >= 0) { if (moveNumber(puzzle, fixes, n, pos1) < 0) { printPuzzle(puzzle); puts("error2"); return; } } else { printPuzzle(puzzle); puts("error3"); return; } continue; } else if ((p1 == m && p2 == 0 && p4 == n) || (p1 == 0 && p2 == n && p3 == m)) { dputs("rare case"); moveRightSpace(puzzle); dputs("OK2"); break; } else if (p1 == n && p3 == m) { int f3 = fixes[pos3.y][pos3.x]; dputs("p1==n&&p3==m"); fixes[pos1.y][pos1.x] = FIXED; fixes[pos3.y][pos3.x] = FIXED; if (searchSpaceRoot(puzzle, fixes, nextmap, pos2) == INT_MAX) { printPuzzle(puzzle); puts("error"); return; } fixes[pos3.y][pos3.x] = f3; moveSpace(puzzle, nextmap, pos2); moveUpSpace(puzzle); moveRightSpace(puzzle); dputs("OK3"); break; } else if (p2 == m && p4 == n) { int f4 = fixes[pos4.y][pos4.x]; dputs("p2==m&&p4==n"); fixes[pos2.y][pos2.x] = FIXED; fixes[pos4.y][pos4.x] = FIXED; if (searchSpaceRoot(puzzle, fixes, nextmap, pos1) == INT_MAX) { printPuzzle(puzzle); puts("error"); return; } fixes[pos4.y][pos4.x] = f4; moveSpace(puzzle, nextmap, pos1); moveDownSpace(puzzle); moveRightSpace(puzzle); dputs("OK4"); break; } else if (p1 == 0 && p2 == m && p3 == n) { moveDownSpace(puzzle); continue; } else if (p1 == n && p2 == 0 && p4 == m) { moveRightSpace(puzzle); moveUpSpace(puzzle); moveLeftSpace(puzzle); moveDownSpace(puzzle); continue; } else if (p1 == n && p2 == m) { searchSpaceRoot(puzzle, fixes, nextmap, pos3); moveSpace(puzzle, nextmap, pos3); moveLeftSpace(puzzle); moveDownSpace(puzzle); continue; } else if (p2 == n && p4 == m) { if (p1 == 0) { moveDownSpace(puzzle); moveRightSpace(puzzle); continue; } else if (p3 == 0) { moveLeftSpace(puzzle); moveDownSpace(puzzle); moveRightSpace(puzzle); continue; } else { Position pos = makePosition(x + 2, y); searchSpaceRoot(puzzle, fixes, nextmap, pos); moveSpace(puzzle, nextmap, pos); moveLeftSpace(puzzle); moveLeftSpace(puzzle); moveDownSpace(puzzle); moveRightSpace(puzzle); continue; } } else if (p1 == m && p3 == n) { Position pos = makePosition(x + 2, y + 1); int f = fixes[pos.y][pos.x]; dputs("wrong case (move n)"); dcode(printTable("fixes", fixes)); if (moveNumber(puzzle, fixes, n, pos) < 0) { printPuzzle(puzzle); printf("error in moveLeft2Numbers(): n pos %d\n", m); return; } dputs("wrong case (move m)"); dcode(printTable("fixes", fixes)); fixes[pos.y][pos.x] = FIXED; if (moveNumber(puzzle, fixes, m, pos2) < 0) { printPuzzle(puzzle); printf("error in moveLeft2Numbers(): m pos2 %d\n", m); return; } fixes[pos.y][pos.x] = f; continue; } else if (p1 == m) { int f1 = fixes[pos1.y][pos1.x]; dputs("p1==m"); fixes[pos1.y][pos1.x] = FIXED; if (searchSpaceRoot(puzzle, fixes, nextmap, pos2) == INT_MAX) { printPuzzle(puzzle); puts("error"); return; } fixes[pos1.y][pos1.x] = f1; moveSpace(puzzle, nextmap, pos2); moveUpSpace(puzzle); continue; } else if (p2 == n) { int f2 = fixes[pos2.y][pos2.x]; dputs("p2==n"); fixes[pos2.y][pos2.x] = FIXED; if (searchSpaceRoot(puzzle, fixes, nextmap, pos1) == INT_MAX) { printPuzzle(puzzle); puts("error"); return; } fixes[pos2.y][pos2.x] = f2; moveSpace(puzzle, nextmap, pos1); moveDownSpace(puzzle); continue; } else if (p1 == n) { int f1 = fixes[pos1.y][pos1.x]; dputs("p1==n"); fixes[pos1.y][pos1.x] = FIXED; if (moveNumber(puzzle, fixes, m, pos3) < 0) { printPuzzle(puzzle); printf("error in moveLeft2Numbers(): p1==n %d\n", m); return; } fixes[pos1.y][pos1.x] = f1; continue; } else if (p2 == m) { int f2 = fixes[pos2.y][pos2.x]; dputs("p2==m"); fixes[pos2.y][pos2.x] = FIXED; if (moveNumber(puzzle, fixes, n, pos4) < 0) { printPuzzle(puzzle); printf("error in moveLeft2Numbers(): p2==m %d\n", m); return; } fixes[pos2.y][pos2.x] = f2; continue; } else { printPuzzle(puzzle); puts("error dayo"); return; } } fixes[pos1.y][pos1.x] = FIXED; fixes[pos2.y][pos2.x] = FIXED; }
int main(int argc, char * * argv) { if (argc < 3) { fprintf(stderr, "Invalid arguments, please refer to " "the README file, or read the source for " "pa11.c\n"); return EXIT_FAILURE; } char * state = argv[2]; if(!isValidState(state)) { fprintf(stderr, "Invalid state: %s\n", state); return EXIT_FAILURE; } if (strcmp(argv[1], "1") == 0) { if (argc < 4) { fprintf(stderr, "Invalid arguments for cmd=1\n"); return EXIT_FAILURE; } const char * movelist = argv[3]; if(!isValidMoveList(movelist)) { fprintf(stderr, "Invalid movelist: %s\n", movelist); return EXIT_FAILURE; } printPuzzle(state); processMoveList(state, movelist); printPuzzle(state); } if (strcmp(argv[1], "2") == 0) { if (argc < 4) { fprintf(stderr, "Invalid arguments for cmd=2\n"); return EXIT_FAILURE; } int nummove = (int) strtol(argv[3], NULL, 10); if ((nummove < 0) || (nummove > 9)) { fprintf(stderr, "Expected n=[0..9], but got %d\n", nummove); return EXIT_FAILURE; } MoveTree * tree = generateAll(state, nummove); MoveTree_print(tree); MoveTree_destroy(tree); } if (strcmp(argv[1], "3") == 0) { char * movelist = solve(state); if(movelist == NULL) { printf("No solution within %d steps\n", MAX_SEARCH_DEPTH); } else { printf("%s\n", movelist); } free(movelist); } return EXIT_SUCCESS; }