-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.cpp
196 lines (173 loc) · 7.11 KB
/
sudoku.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
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <fstream>
#include <vector>
using namespace std;
///////////////////////////////////////////////////////////////////////////////////////
// IO funkcijos
void get_board_from_file(vector<int>[9][9], char[]);
void print_board_to_file(vector<int>[9][9], char[]);
void print_error(vector<int>[9][9], char[], int, int);
// Funkcijos sprendziancios sudoku
void initialize_board(vector<int>[9][9]);
void logic_solve(vector<int>[9][9]);
bool backtrack(vector<int>[9][9]);
bool check_if_doable(vector<int>[9][9], int&, int&);
// Pagalbines funkcijos, dazniausiai dirbancios su konkreciais langeliais
bool is_number_available_at(vector<int>[9][9], int rows, int cols, int);
void write_number_at(vector<int>[9][9], int rows, int cols, int);
int unknown_squares(vector<int>[9][9]);
///////////////////////////////////////////////////////////////////////////////////////
int main(){
vector<int> board[9][9];
get_board_from_file(board, "sudokuIn.txt"); // Input
initialize_board(board); // Sudoku tvarkomas, atmetami kai kurie
logic_solve(board); // logiskai netinkantys variantai
int rows, cols;
if (!check_if_doable(board, rows, cols)) // Jeigu paduoti duomenys akivaizdziai neteisingi isvedamas erroras
print_error(board, "sudokuOut.txt", rows, cols);
else if (backtrack(board)) // Kitu atveju naudojamas brute force ir gautas atsakymas isvedamas
print_board_to_file(board, "sudokuOut.txt");
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
// IO funkcijos
// Nuskaito pradinius duotuosius sudoku skaiciukus
void get_board_from_file(vector<int> board[9][9], char filename[]){
ifstream sudokuIn;
sudokuIn.open(filename);
int tempval;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++){
sudokuIn >> tempval;
board[i][j].push_back(tempval);
}
sudokuIn.close();
}
// Isveda Sudoku i faila, jei pradiniai duomenys yra teisingi
void print_board_to_file(vector<int> board[9][9], char filename[]){
ofstream sudokuOut;
sudokuOut.open(filename);
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++)
sudokuOut << board[i][j][0] << " " ;
sudokuOut << endl;
}
sudokuOut.close();
}
// Jei sudoku yra klaidingas tam tikrame langelyje, tai isvedama klaida ir langelio koordinates
void print_error(vector<int> board[9][9], char filename[], int i, int j){
ofstream sudokuOut;
sudokuOut.open(filename);
sudokuOut << "Klaida " << i+1 << ", " << j+1 << " langelyje";
sudokuOut.close();
}
///////////////////////////////////////////////////////////////////////////////////////
// Funkcijos sprendziancios sudoku
// Suranda visas galimas reiksmes tusciuose langeliuose
void initialize_board(vector<int> board[9][9]){
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if(board[i][j][0] == 0)
for(int k = 1; k < 10; k++)
if(is_number_available_at(board, i, j, k))
board[i][j].push_back(k);
}
// Isprendzia visus langelius, kuriuose galima tik viena reiksme
void logic_solve(vector<int> board[9][9]){
int unsolved;
do{
unsolved = unknown_squares(board);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if(board[i][j].size() == 2)
write_number_at(board, i, j, board[i][j][1]);
} while(unsolved != unknown_squares(board));
}
// Rekursine funkcija, kuri ima logiskas reiksmes ir backtrackina
bool backtrack(vector<int> board[9][9]){
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if(board[i][j][0] == 0){
for(int k = 1; k < board[i][j].size(); k++){
if(is_number_available_at(board, i, j, board[i][j][k])){
board[i][j][0] = board[i][j][k];
board[i][j][k] = 0;
if(backtrack(board)){
return true;
} else {
board[i][j][k] = board[i][j][0];
board[i][j][0] = 0;
}
}
}
return false;
}
return true;
}
// Patikrina ar lenta nera akivaizdziai neissprendziama
bool check_if_doable(vector<int> board[9][9], int& rows, int& cols){
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if(board[i][j][0] != 0){
if(!is_number_available_at(board, i, j, board[i][j][0])){
rows = i;
cols = j;
return false;
}
} else if(board[i][j].size() == 1){
rows = i;
cols = j;
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////
// Pagalbines funkcijos, dazniausiai dirbancios su konkreciais langeliais
// Suskaiciuoja kiek boarde yra nuliuku
int unknown_squares(vector<int> board[9][9]){
int counter = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if(board[i][j][0] == 0)
counter++;
return counter;
}
// I langeli iraso skaiciu ir pasalina reiksmes, kurios kertasi su siuo skaiciu eiluteje, stulpelyje ir devynete
void write_number_at(vector<int> board[9][9], int rows, int cols, int number){
board[rows][cols].clear();
board[rows][cols].push_back(number);
for(int i = 0; i < 9; i++)
if(board[i][cols][0] == 0)
for(int k = board[i][cols].size() - 1; k > 0; k--)
if(board[i][cols][k] == number)
board[i][cols].erase(board[i][cols].begin()+k);
for(int i = 0; i < 9; i++)
if(board[rows][i][0] == 0)
for(int k = board[rows][i].size() - 1; k > 0; k--)
if(board[rows][i][k] == number)
board[rows][i].erase(board[rows][i].begin()+k);
rows = (rows / 3) * 3;
cols = (cols / 3) * 3;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(board[rows+i][cols+j][0] == 0)
for(int k = board[rows+i][cols+j].size() - 1; k > 0; k--)
if(board[rows+i][cols+j][k] == number)
board[rows+i][cols+j].erase(board[rows+i][cols+j].begin()+k);
}
// Patikrina ar reiksme galima konkreciame langelyje
bool is_number_available_at(vector<int> board[9][9], int rows, int cols, int number){
for(int i = 0; i < 9; i++)
if(board[i][cols][0] == number && i != rows)
return false;
for(int i = 0; i < 9; i++)
if(board[rows][i][0] == number && i != cols)
return false;
int top_row_pt = (rows / 3) * 3;
int top_col_pt = (cols / 3) * 3;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(board[top_row_pt+i][top_col_pt+j][0] == number)
if(top_row_pt+i != rows && top_col_pt+j != cols)
return false;
return true;
}