https://github.com/tasxatzial/sudoku-solver
Generate and solve standard sudoku puzzles
https://github.com/tasxatzial/sudoku-solver
sudoku sudoku-generator sudoku-solver
Last synced: 7 months ago
JSON representation
Generate and solve standard sudoku puzzles
- Host: GitHub
- URL: https://github.com/tasxatzial/sudoku-solver
- Owner: tasxatzial
- License: mit
- Created: 2020-10-05T21:42:33.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-06T20:15:35.000Z (over 1 year ago)
- Last Synced: 2025-01-13T03:09:57.679Z (9 months ago)
- Topics: sudoku, sudoku-generator, sudoku-solver
- Language: C
- Homepage:
- Size: 104 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sudoku library
Library for generating and solving [Sudoku](https://en.wikipedia.org/wiki/Sudoku) puzzles.
The following functions are provided:
* sudoku_read(): Read a puzzle
* sudoku_print(puzzle): Print puzzle
* sudoku_print_errors(puzzle): Print rule-related errors of puzzle
* sudoku_print_empty(puzzle): Print the empty cells of puzzle
* sudoku_format_is_correct(puzzle): Show whether puzzle meets the required format
* sudoku_is_correct(puzzle): Check whether puzzle has issues (rules violation, empty cells)
* sudoku_solve(puzzle): Solve puzzle
* sudoku_generate(N): Generate puzzle that has N non empty cells
* sudoku_has_unique_choice_solution(puzzle): Show whether puzzle has a unique choice solution
* sudoku_set_value(puzzle, row, col, val): Set cell value
* sudoku_clear_value(puzzle, row, col): Clear cell value
* sudoku_set_choice(puzzle, row, col, val): Set val as cell choice
* sudoku_clear_choice(puzzle, row, col, val): Clear val from cell choices## Implementation
Sudokus are solved using a [backtracking](https://en.wikipedia.org/wiki/Backtracking) algorithm.
## Compile
* Build the library (functions declared in sudoku.h):
```bash
make sudoku.o
```* Build the UI that uses the library:
```bash
make sudoku-ui
```## Typical usage
* Read a sudoku from input_file and solve it:
```bash
./sudoku-ui < input_file
```* Read a sudoku from input_file and check its correctness:
```bash
./sudoku-ui -c < input_file
```* Read a sudoku from input_file and print it in a 9x9 grid:
```bash
./sudoku-ui -s < input_file
```* Generate a solvable sudoku with 40 non-zero numbers:
```bash
./sudoku-ui -g 40
```* Generate a solvable sudoku with 40 non-zero numbers and solve it:
```bash
./sudoku-ui -g 40 | ./sudoku-ui
```Note: When the non-zero count is very low, it may not be possible to return a puzzle with a unique available choice at each step of the solution. Consequently, such puzzles may have multiple solutions. In such cases, the solver will provide only one of the possible solutions.
## Puzzles
[Puzzles](puzzles/) folder contains a selection of unsolved puzzles.
## Profiling
'sudoku-ui' has been tested for memory leaks with [valgrind](https://valgrind.org/) and [AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer).