-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
165 lines (128 loc) · 3.54 KB
/
main.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
// 24.02.24
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <chrono>
// Variable
int operations = 0;
const int SIZE = 9;
// Creation
int** create_table() {
int** sudoku = new int* [SIZE];
for (int i = 0; i < SIZE; ++i) {
sudoku[i] = new int[SIZE];
}
// Initialize the Sudoku grid
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
sudoku[i][j] = 0;
}
}
return sudoku;
}
void create(int**& sudoku) {
// 1)
int values[9][9] = {
{0, 0, 0, 6, 3, 4, 7, 0, 0},
{7, 0, 6, 0, 0, 0, 0, 0, 9},
{0, 0, 0, 0, 0, 5, 0, 8, 0},
{0, 7, 0, 0, 2, 0, 0, 0, 3},
{8, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 3, 0, 0, 1, 0, 0, 7, 0},
{0, 5, 0, 2, 0, 0, 0, 0, 0},
{3, 0, 0, 0, 0, 0, 2, 0, 8},
{0, 0, 2, 3, 0, 9, 0, 0, 0}
};
// Copy values from the temporary array to the Sudoku grid
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
sudoku[i][j] = values[i][j];
}
}
}
void show(int** sudoku) {
for (int i = 0; i < SIZE; ++i) {
if (i % 3 == 0 && i != 0) {
std::cout << "------+-------+------" << std::endl;
}
for (int j = 0; j < SIZE; ++j) {
if (j % 3 == 0 && j != 0) {
std::cout << "| ";
}
std::cout << sudoku[i][j] << " ";
}
std::cout << std::endl;
}
}
void clean_memory(int** sudoku) {
for (int i = 0; i < SIZE; ++i) {
delete[] sudoku[i];
}
delete[] sudoku;
}
// Logic
bool is_cell_valid(int**& sudoku, int row, int col, int number) {
// Check for row
for (int x = 0; x <= 8; x++)
if (sudoku[row][x] == number)
return false;
// Check for column
for (int x = 0; x <= 8; x++)
if (sudoku[x][col] == number)
return false;
// Check if we find the same num in 3x3 cell
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (sudoku[i + startRow][j + startCol] == number)
return false;
return true;
}
bool solve(int** sudoku, int row, int col) {
// Check end
if (row == SIZE - 1 && col == SIZE)
return true;
// Check if column value becomes 9
if (col == SIZE) {
row++;
col = 0;
}
// Check if the current position is > 0
if (sudoku[row][col] > 0)
return solve(sudoku, row, col + 1);
for (int num = 1; num <= SIZE; num++){
// Check if it is safe to place
if (is_cell_valid(sudoku, row, col, num)){
operations++;
sudoku[row][col] = num;
// Checking for next possibility with next
if (solve(sudoku, row, col + 1))
return true;
}
// Backtracking
operations++;
sudoku[row][col] = 0;
}
return false;
}
// Main
int main() {
auto start = std::chrono::high_resolution_clock::now();
int** sudoku = create_table();
create(sudoku);
if (solve(sudoku, 0, 0)) {
printf("Solve \n");
}
else {
printf("Cant solve \n");
}
show(sudoku);
clean_memory(sudoku);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
printf("\nOperations: %d \n", operations);
std::cout << "\nDuration: " << duration.count() << " microseconds" << std::endl;
return 0;
}