-
Notifications
You must be signed in to change notification settings - Fork 359
/
sudoku_solver.cpp
168 lines (156 loc) · 2.68 KB
/
sudoku_solver.cpp
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
#include <iostream>
#include <fstream>
using namespace std;
void getboard(int solved[2]);
bool checkboard(int board[][9]);
bool checkrow(int board[][9]);
bool checkcol(int board[][9]);
bool checkbox(int board[][9]);
int main()
{
int solved[2] = {0};
cout << "Welcome please wait while I check your boards...\n";
getboard(solved);
cout << "Very good you solved: " << solved[0]+solved[1] << " boards.\n"
<< solved[1] << " boards were right.\n"
<< solved[0] << " boards were wrong.\n";
return 0;
}
void getboard(int solved[2])
{
ifstream sudokuIn("sudoku_boards.txt");
while (!sudokuIn.eof())
int board[9][9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
sudokuIn >> board[i][j];
}
}
if (checkboard(board))
solved[1]++;
else
solved[0]++;
}
}
bool checkboard(int board[9][9])
{
bool solved = false;
solved = checkrow(board);
if (solved == false)
solved = checkcol(board);
if (solved == false)
solved = checkbox(board);
return !solved;
}
bool checkrow(int board[][9])
{
bool solved = false;
int row[9];
for (int x = 0; x < 9; x++)
row[x] = 0;
for (int i = 0; i < 9 && !solved; i++)
{
for (int j = 0; j < 9; j++)
{
row[board[i][j]-1]++;
}
for (int x = 0; x < 9; x++)
{
if (row[x] != 1)
{
solved = true;
}
}
for (int x = 0; x < 9; x++)
row[x] = 0;
}
return solved;
}
bool checkcol(int board[][9])
{
bool solved = false;
int col[9];
for (int i = 0; i < 9; i++)
col[i] = 0;
for (int i = 0; i < 9 && !solved; i++)
{
for (int j = 0; j < 9; j++)
{
col[board[j][i]-1]++;
}
for (int x = 0; x < 9; x++)
{
if (col[x] != 1)
{
solved = true;
}
}
for (int x = 0; x < 9; x++)
col[x] = 0;
}
return solved;
}
bool checkbox(int board[][9])
{
bool solved = false;
int box[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
box[i][j] = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
box[0][board[i][j]-1]++;
}
for (int j = 3; j < 6; j++)
{
box[1][board[i][j]-1]++;
}
for (int j = 6; j < 9; j++)
{
box[2][board[i][j]-1]++;
}
}
for (int i = 3; i < 6; i++)
{
for (int j = 0; j < 3; j++)
{
box[3][board[i][j]-1]++;
}
for (int j = 3; j < 6; j++)
{
box[4][board[i][j]-1]++;
}
for (int j = 6; j < 9; j++)
{
box[5][board[i][j]-1]++;
}
}
for (int i = 6; i < 9; i++)
{
for (int j = 0; j < 3; j++)
{
box[6][board[i][j]-1]++;
}
for (int j = 3; j < 6; j++)
{
box[7][board[i][j]-1]++;
}
for (int j = 6; j < 9; j++)
{
box[8][board[i][j]-1]++;
}
}
for (int i = 0; i < 9 && !solved; i++)
{
for (int j = 0; j < 9; j++)
{
if (box[i][j] != 1)
solved = true;
}
}
return solved;
}