-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.c
159 lines (138 loc) · 3.82 KB
/
Sudoku.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <sys/time.h>
#define PATH "puzzles/puzzle.txt"
int64_t currentTimeMillis() {
struct timeval time;
gettimeofday(&time, NULL);
int64_t s1 = (int64_t)(time.tv_sec) * 1000;
int64_t s2 = (time.tv_usec / 1000);
return s1 + s2;
}
const int size = 9;
// return 0 for invalid, 1 for valid
int validateRow(int index, int** board) {
int col = index % size;
int row = index / size;
int num = board[row][col];
for (int i = 0; i < size; i++) {
if (i != col && board[row][i] == num) {
return 0;
}
}
return 1;
}
// return 0 for invalid, 1 for valid
int validateCol(int index, int** board) {
int col = index % size;
int row = index / size;
int num = board[row][col];
for (int i = 0; i < size; i++) {
if (i != row && board[i][col] == num) {
return 0;
}
}
return 1;
}
// return 0 for invalid, 1 for valid
int validateSquare(int index, int** board) {
int col = index % size;
int row = index / size;
int num = board[row][col];
const int root = 3;
int srow = 1 + root * (row / root);
int scol = 1 + root * (col / root);
//iterate through the 3x3 square the number is in
for (int i = srow - 1; i <= srow + 1; i++) {
for (int j = scol - 1; j <= scol + 1; j++) {
if (!(i == row && j == col) && board[i][j] == num) {
return 0;
}
}
}
return 1;
}
// return 0 for invalid, 1 for valid
int isValidNum(int index, int** board) {
return validateRow(index, board) &&
validateCol(index, board) &&
validateSquare(index, board);
}
int** test(int index, int** board) {
if (index >= 81) {
return board;
} else if (board[index / size][index % size] > 0) {
return test(index + 1, board);
}
//allocate ram for temp
int** temp = (int**)(calloc(size, sizeof(int*)));
for (int i = 0; i < size; i++) {
temp[i] = (int*)(calloc(size, sizeof(int)));
}
//copy board to temp
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
temp[row][col] = board[row][col];
}
}
//change temp
//test the change to see if board is still valid
for (int i = 1; i <= 9; i++) {
temp[index / size][index % size] = i;
if (isValidNum(index, temp)) {
int** possible = test(index + 1, temp);
if (possible != NULL) {
return possible;
}
}
}
return NULL;
}
//print board
void printBoard(int** board) {
if (board == NULL) {
printf("Board is null\n");
return;
}
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
printf("[%d]", board[row][col]);
}
printf("\n");
}
printf("\n");
}
//read contents of puzzle.txt file
void readFile(int** board) {
FILE* file = fopen(PATH, "r");
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
int num = fgetc(file) - '0';
if (num < 0 || num > 9) {
printf("puzzle.txt corrupted\n");
exit(1);
}
board[row][col] = num;
}
}
fclose(file);
}
int main() {
//allocate memory for sudoku board
int** board = (int**)(calloc(size, sizeof(int*)));
for (int i = 0; i < size; i++) {
board[i] = (int*)(calloc(size, sizeof(int)));
}
int index;
readFile(board);
printBoard(board);
//board[0][2] = 3;
//printf("%d\n", isValidNum(2, board) ? 10 : 11);
int64_t beg = currentTimeMillis();
printBoard(test(0, board));
printf("Time elapsed: %d milliseconds\n", currentTimeMillis() - beg);
return 0;
}