-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku-solver.cpp
183 lines (168 loc) · 4.59 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const int VELICINA = 9;
void printPloca(vector<vector<int>>& board) {
cout << " ";
for (int i = 0; i < VELICINA; ++i) {
cout << i + 1 << " ";
if ((i + 1) % 3 == 0 && i != VELICINA - 1) {
cout << "| ";
}
}
cout << endl << " -";
for (int i = 0; i < VELICINA; ++i) {
cout << "--";
if ((i + 1) % 3 == 0 && i != VELICINA - 1) {
cout << "-";
}
}
cout << endl;
for (int i = 0; i < VELICINA; ++i) {
cout << i + 1 << "|";
for (int j = 0; j < VELICINA; ++j) {
cout << board[i][j] << " ";
if ((j + 1) % 3 == 0 && j != VELICINA - 1) {
cout << "| ";
}
}
cout << endl;
if ((i + 1) % 3 == 0 && i != VELICINA - 1) {
cout << " -";
for (int j = 0; j < VELICINA; ++j) {
cout << "--";
if ((j + 1) % 3 == 0 && j != VELICINA - 1) {
cout << "-";
}
}
cout << endl;
}
}
}
bool isRowValid(vector<vector<int>>& board, int row) {
bool used[VELICINA + 1] = { false };
for (int i = 0; i < VELICINA; ++i) {
int val = board[row][i];
if (val == 0) {
continue;
}
if (used[val]) {
return false;
}
used[val] = true;
}
return true;
}
bool isColValid(vector<vector<int>>& board, int col) {
bool used[VELICINA + 1] = { false };
for (int i = 0; i < VELICINA; ++i) {
int val = board[i][col];
if (val == 0) {
continue;
}
if (used[val]) {
return false;
}
used[val] = true;
}
return true;
}
bool isBoxValid(vector<vector<int>>& board, int row, int col) {
bool used[VELICINA + 1] = { false };
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = startRow; i < startRow + 3; ++i) {
for (int j = startCol; j < startCol + 3; ++j) {
int val = board[i][j];
if (val == 0) {
continue;
}
if (used[val]) {
return false;
}
used[val] = true;
}
}
return true;
}
bool isValid(vector<vector<int>>& board, int row, int col) {
return isRowValid(board, row) && isColValid(board, col) && isBoxValid(board, row, col);
}
bool solveSudoku(vector<vector<int>>& board, int row, int col) {
if (row == VELICINA) {
return true;
}
if (col == VELICINA) {
return solveSudoku(board, row + 1, 0);
}
if (board[row][col] != 0) {
return solveSudoku(board, row, col + 1);
}
for (int val = 1; val <= 9; ++val) {
board[row][col] = val;
if (isValid(board, row, col) && solveSudoku(board, row, col + 1)) {
return true;
}
}
board[row][col] = 0;
return false;
}
void generisiPlocu(vector<vector<int>>& board) {
srand(time(NULL));
int numCells = rand() % 10 + 20;
for (int i = 0; i < numCells; ++i) {
int row = rand() % 9;
int col = rand() % 9;
int val = rand() % 9 + 1;
if (board[row][col] != 0) {
continue;
}
board[row][col] = val;
if (!isValid(board, row, col)) {
board[row][col] = 0;
}
}
}
bool isBoardFull(vector<vector<int>>& board) {
for (int i = 0; i < VELICINA; ++i) {
for (int j = 0; j < VELICINA; ++j) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}
bool isBoardValid(vector<vector<int>>& board) {
for (int i = 0; i < VELICINA; ++i) {
if (!isRowValid(board, i)) {
return false;
}
if (!isColValid(board, i)) {
return false;
}
}
for (int i = 0; i < VELICINA; i += 3) {
for (int j = 0; j < VELICINA; j += 3) {
if (!isBoxValid(board, i, j)) {
return false;
}
}
}
return true;
}
int main() {
vector<vector<int>> board(VELICINA, vector<int>(VELICINA, 0));
generisiPlocu(board);
printPloca(board);
cout << endl << "Rjesenje:" << endl;
if (solveSudoku(board, 0, 0)) {
printPloca(board);
}
else {
cout << "Nema rjesenja za Sudoku." << endl;
}
return 0;
}