Here's some code to read in a grid and an input file with 5 grids. My simple program seems to solve all of them more or less instantly, so maybe sudoku is computationally easy? I thought it was potentially quite difficult. Except for the wikipedia puzzle, the puzzles are from Play Free Sudoku online - solve web sudoku puzzles. To select a particular puzzle, enter it's offset as a command line parameter.
Input file:Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void read(int grid[][9], int pos) {
FILE *f = fopen("sudoku.txt", "r");
char line[256];
for (int i = 0; fgets(line, sizeof line, f); )
if (strncmp(line, "===", 3) == 0 && i++ == pos) break;
if (feof(f)) {
fprintf(stderr, "Puzzle %d does not exist\n", pos);
exit(EXIT_FAILURE);
}
for (int r = 0; r < 9; ++r)
for (int c = 0; c < 9; ++c) {
int n = 0;
fscanf(f, "%d", &n); // fscanf will eat the dash if present
grid[r][c] = n; // n is 0 if no number was read
}
fclose(f);
}
void print(int grid[][9]) {
for (int r = 0; r < 9; ++r) {
for (int c = 0; c < 9; ++c) {
if (c && c % 3 == 0) printf(" ");
if (grid[r][c])
printf("%d ", grid[r][c]);
else
printf("- ");
}
putchar('\n');
if (r < 8 && r % 3 == 2) putchar('\n');
}
}
int main(int argc, char **argv) {
int pos = 0;
if (argc == 2) pos = atoi(argv[1]);
int grid[9][9];
read(grid, pos);
print(grid);
return 0;
}
Code:=== wikipedia 0
5 3 - - 7 - - - -
6 - - 1 9 5 - - -
- 9 8 - - - - 6 -
8 - - - 6 - - - 3
4 - - 8 - 3 - - 1
7 - - - 2 - - - 6
- 6 - - - - 2 8 -
- - - 4 1 9 - - 5
- - - - 8 - - 7 9
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
=== easy 1
5 - - - 1 6 2 - -
- - - 9 8 - 5 - 4
- 3 - - - - - - 9
- - - 6 - - 1 - -
9 - 6 - - - - - 2
- 2 3 - 4 9 - - -
- 4 - - - - - 8 1
3 1 5 2 9 - - 6 7
8 - 9 4 7 1 - 2 5
5 9 4 3 1 6 2 7 8
6 7 1 9 8 2 5 3 4
2 3 8 7 5 4 6 1 9
4 8 7 6 2 5 1 9 3
9 5 6 1 3 7 8 4 2
1 2 3 8 4 9 7 5 6
7 4 2 5 6 3 9 8 1
3 1 5 2 9 8 4 6 7
8 6 9 4 7 1 3 2 5
=== expert 2
- - - 5 - 6 - - -
- - - - - - - 6 2
7 - 4 - 1 - - - -
9 - - - 4 3 - - 7
- 2 - - - - - - -
- 8 - - 9 - - 5 -
- - - - - - - - -
3 - - - - 8 7 - -
- 5 - 3 - 4 - - 1
1 3 2 5 8 6 4 7 9
5 9 8 4 3 7 1 6 2
7 6 4 9 1 2 5 3 8
9 1 5 2 4 3 6 8 7
4 2 7 8 6 5 9 1 3
6 8 3 7 9 1 2 5 4
8 7 1 6 2 9 3 4 5
3 4 9 1 5 8 7 2 6
2 5 6 3 7 4 8 9 1
=== evil 3
- 9 3 4 7 - - 6 -
- 8 - - - - - - -
- - - 6 - - - - 1
8 - - - - - - 3 -
- 3 4 - - 9 - - 5
1 - - - 4 - - - -
- - - - - 5 2 - -
- 6 7 - 9 - - 1 -
4 - - - - - - - -
5 9 3 4 7 1 8 6 2
6 8 1 9 5 2 4 7 3
7 4 2 6 3 8 9 5 1
8 7 9 5 2 6 1 3 4
2 3 4 7 1 9 6 8 5
1 5 6 8 4 3 7 2 9
9 1 8 3 6 5 2 4 7
3 6 7 2 9 4 5 1 8
4 2 5 1 8 7 3 9 6
=== evil 4
5 - - - 7 8 - - -
- - - - - - 4 - -
- - 8 - 9 - 1 - 7
- - 7 - - - - 4 -
2 - - 1 - - 7 - 6
- - - - 6 - - 5 -
- 3 - 2 - - - - -
- - 5 - 1 - 8 - 9
- - - - - - - 6 -
5 1 2 4 7 8 6 9 3
7 6 9 3 2 1 4 8 5
3 4 8 5 9 6 1 2 7
6 5 7 8 3 2 9 4 1
2 8 4 1 5 9 7 3 6
1 9 3 7 6 4 2 5 8
9 3 6 2 8 7 5 1 4
4 2 5 6 1 3 8 7 9
8 7 1 9 4 5 3 6 2