This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
179 lines (146 loc) · 3.88 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "main.h"
#include "filemanager.h"
#include "board.h"
#define EMPTY 0
int main(int argc, char** argv)
{
if (argc == 3)
{
int*** board;
int size;
/* parse size argument from command-line to integer */
size = atoi(argv[2]);
/* create a board of specified size, this will need to be freed */
board = create_board(size);
/* read board from file passed in command-line */
read_board(board, argv[1], size);
if (solve(board, size))
{
#ifndef QUIET
/* board was solved, display solution */
display_board(board, size);
#endif
}
else
{
/* board could not be solved */
printf("No solution\n");
}
/* free board memory */
free_board(board, size);
board = NULL;
}
else
{
/* incorrect number of arguments */
printf("Usage: ./solve filename size\n");
}
return 0;
}
int solve(int*** board, int size)
{
int solved, num, row, col;
row = 0;
col = 0;
if (find_empty_space(board, &row, &col, size))
{
/* an empty space was found, row and col were updated */
solved = 0;
/* iterate through numbers 1 to size while board not solved */
num = 1;
while (!solved && num < size + 1)
{
if (valid_placement(board, row, col, num, size))
{
/* can place number in position */
#ifdef VERBOSE
printf("%d, %d, %d\n", row, col, num);
#endif
/* update board with number in position */
(*board)[row][col] = num;
/* call this function */
solved = solve(board, size);
if (!solved)
{
/* placing this number did not lead to a solution
* so we remove it */
(*board)[row][col] = EMPTY;
}
}
/* increment number */
num++;
}
}
else
{
/* no empty places, board must be solved */
solved = 1;
}
return solved;
}
int valid_placement(int*** board, int row, int col, int num, int size)
{
int found, i, j, r;
found = 0;
/* while number not found, check each column in row */
i = 0;
while (!found && i < size)
{
found = (*board)[row][i] == num;
i++;
}
/* while number not found, check each row in column */
j = 0;
while (!found && j < size)
{
found = (*board)[j][col] == num;
j++;
}
/* r is the square root of board size */
r = (int)sqrt(size);
/* the value of 'i' will be added to row */
i = 0;
while (!found && i < r)
{
/* the value of 'j' will be added to col */
j = 0;
while (!found && j < r)
{
/* row - (row % r) is the first row in the box */
found = (*board)[row - (row % r) + i][col - (col % r) + j] == num;
j++;
}
i++;
}
return !found;
}
int find_empty_space(int*** board, int* row, int* col, int size)
{
int found, i, j;
/* we haven't found an empty space yet */
found = 0;
/* while empty space not found, iterate through 0 to size */
i = 0;
while (!found && i < size)
{
/* while empty space not found, iterate through 0 to size */
j = 0;
while (!found && j < size)
{
if ((*board)[i][j] == EMPTY)
{
/* position is empty space, set found to true */
found = 1;
/* update imported row and col pointers */
*row = i;
*col = j;
}
j++;
}
i++;
}
return found;
}