-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedin.cpp
More file actions
196 lines (145 loc) · 4.86 KB
/
edin.cpp
File metadata and controls
196 lines (145 loc) · 4.86 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <functional>
#include <iostream>
#include <fstream>
#include <chrono>
#include <vector>
#include <limits>
//Main sudoku vector
//9x9 sudoku bulmacalarını depolayacak vektör.
//IMPORTANT: If you want to use with higher performance, does not change anything.
//IMPORTANT: For slower performance, activate the std::vector instead.
//std::vector<std::vector<int>> su(9, std::vector<int>(9));
int su[9][9];
//Variables
static int current_puzzle = 0, solutions = 1;
//Jump to the specific line in multiple sudoku puzzle entry cases.
//Dosyadan birçok sudoku bulmacası çözmek istenildiğinde bu kod,
//belirtilen satıra atlar.
std::fstream& jump(std::fstream& file, unsigned int line) {
std::string buffer;
file.seekg(std::ios::beg);
for (unsigned int i = 1; i < line; ++i) {
std::getline(file, buffer);
if (file.eof()) {
file.clear();
break;
}
}
return file;
}
//Determine the mid square of the 3x3 matrix.
//Sudokuyu 3x3'lük matrislere bölersek, her bir matrisin
//ortasındaki kareyi işaret eden bir fonksiyon.
//Örn: [1][1], sol üstteki 3x3 karenin orta karesidir.
int mid_square(int d) {
return ((d / 3) * 3);
}
//Check if the number already exists in entire column or row.
//Belirtilen sayı aynı satın ya da sütunda mevcut mu?
int check(int r, int c, int d) {
for(int i=0; i<9; i++) {
if(su[r][i] == d || su[i][c] == d) {
return 0;
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(su[mid_square(r) + i][mid_square(c) + j] == d) {
return 0;
}
}
}
//Eğer bu fonksiyon yukarıdaki döngülerden sonra 0 dönmediyse, belirtilen sayı o kareye uygundur.
return 1;
}
//Print the result.
//Sonucu ekrana yazdırır.
void print() {
std::cout << solutions << ".solution:\n\n";
for (const auto& p : su) {
for (const auto& pr : p) {
std::cout << pr << " ";
}
std::cout << std::endl;
}
solutions++;
std::cout << std::endl;
}
//Main evaluation. Using backtracking algorithm.
//Çözücü fonksiyonumuz budur. Backtracking algoritması kullanıyoruz.
//Yani birden ona kadar olan sayıları tek tek check() ettirip uygun olanı koyar.
void eval() {
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
if(!su[i][j]) {
for(int k=1; k<10; k++) {
if(check(i, j, k)) {
//Assign first.
su[i][j] = k;
//Backtracking.
eval();
//If any number fits, then assign back to zero.
su[i][j] = 0;
}
}
return;
}
}
}
print();
}
//Check the arguments.
//Program konsol tabanlı olduğundan, parametre olarak kaç adet bulmacanın girilmesi gerektiği bulmalıyız.
void check_args(int a, char* b[]) {
if(a != 3 || std::string(b[1]).find_first_not_of("0123456789") != std::string::npos) {
std::cerr << "Wrong argument. Program ends.\nHatalı giriş." << std::endl;
}
//argv[2] is the path of the puzzle file.
std::fstream sudoku(b[2], std::ios::in);
sudoku.seekg(0, std::ios::end);
if(!sudoku || !sudoku.tellg()) {
sudoku.close();
std::cerr << "File or puzzles not found. Program ends." << std::endl;
}
}
//Count time.
//Bulmacaları toplamda kaç ms'de çözdüğünü sapta.
void magic(std::function<void()> s) {
auto f = std::chrono::high_resolution_clock::now();
s();
auto l = std::chrono::high_resolution_clock::now();
//Find difference.
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(l - f);
//Print each puzzle's solve time.
std::cout << "This puzzle has been solved in "
<< diff.count()
<< "ms."
<< std::endl;
}
int main(int argc, char* argv[]) {
//Check if everything's okey.
check_args(argc, argv);
//Solve.
for(int i=1; i<std::stoi(argv[1]) + 1; i++) {
//Read from file.
std::fstream sudoku(argv[2], std::ios::in);
//Reset.
solutions = 1;
//Jump to the specific line.
jump(sudoku, (10 * i) - 9);
//Fill vector with the current puzzle.
for(int j=0; j<9; j++) {
for(int k=0; k<9; k++) {
sudoku >> su[j][k];
}
}
//To know which puzzle we are in, we must increase this value after each solution.
current_puzzle++;
//Evaluate.
std::cout << "\n\nPuzzle "
<< current_puzzle
<< "\n----------\n";
magic(eval);
}
return 0;
}