-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.cpp
202 lines (185 loc) · 5.35 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
197
198
199
200
201
202
#include <bits/stdc++.h>
using namespace std;
#define order 2
#define length order *order
class game
{
int matrix[order][order][order][order];
bool bit_matrix[order][order][order][order][length];
public:
void take_input()
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
int a;
cin >> a;
matrix[i % order][j % order][i / order][j / order] = a;
}
}
}
void print_matrix()
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
cout << matrix[i % order][j % order][i / order][j / order];
}
cout << endl;
}
}
void print_bit_matrix(int n)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
cout << (bit_matrix[i % order][j % order][i / order][j / order][n - 1] == true) ? 1 : 0;
}
cout << endl;
}
}
void print_mini_matrix(int k, int l)
{
for (int i = 0; i < order; i++)
{
for (int j = 0; j < order; j++)
{
cout << matrix[i][j][k][l];
}
cout << endl;
}
}
void print_horz_matrix(int i)
{
for (int l = 0; l < length; l++)
{
cout << matrix[i % order][l % order][i / order][l / order];
}
}
void print_vertical_matrix(int j)
{
for (int l = 0; l < length; l++)
{
cout << matrix[l % order][j % order][l / order][j / order];
}
}
void set_all_bit_matrix_posn_0(int i, int j)
{
for (int n = 0; n < length; n++)
{
bit_matrix[i % order][j % order][i / order][j / order][n] = 0;
}
}
void reset_bit_matrix()
{
for (int n = 0; n < length; n++)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
bit_matrix[i % order][j % order][i / order][j / order][n] = 1;
}
}
}
}
bool can_go(int i, int j, int n)
{
return bit_matrix[i % order][j % order][i / order][j / order][n - 1];
}
void initiate_bit_matrix()
{
reset_bit_matrix();
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
int n = matrix[i % order][j % order][i / order][j / order];
if (n != 0)
{
set_all_bit_matrix_posn_0(i, j);
// along horizontal
for (int l = 0; l < length; l++)
{
bit_matrix[i % order][l % order][i / order][l / order][n - 1] = 0;
}
// along vertical
for (int l = 0; l < length; l++)
{
bit_matrix[l % order][j % order][l / order][j / order][n - 1] = 0;
}
// int mini matrix
for (int i_temp = 0; i_temp < order; i_temp++)
{
for (int j_temp = 0; j_temp < order; j_temp++)
{
bit_matrix[i_temp][j_temp][i / order][j / order][n - 1] = 0;
}
}
}
}
}
}
bool solve(int i, int j)
{
initiate_bit_matrix();
if (i == length - 1 && j == length)
{
return true;
}
if (j == length)
{
j = 0;
i++;
}
if (matrix[i % order][j % order][i / order][j / order] != 0)
{
return solve(i, j + 1);
}
for (int possible = 1; possible <= length; possible++)
{
if (can_go(i, j, possible))
{
matrix[i % order][j % order][i / order][j / order] = possible;
bool bit_matrix_temp[order][order][order][order][length];
for (int n = 0; n < length; n++)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
bit_matrix_temp[i % order][j % order][i / order][j / order][n] = bit_matrix[i % order][j % order][i / order][j / order][n];
}
}
}
initiate_bit_matrix();
if (solve(i, j + 1))
{
return true;
}
for (int n = 0; n < length; n++)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
bit_matrix[i % order][j % order][i / order][j / order][n] = bit_matrix_temp[i % order][j % order][i / order][j / order][n];
}
}
}
}
matrix[i % order][j % order][i / order][j / order] = 0;
}
return false;
}
};
int main()
{
game A;
A.take_input();
A.solve(0, 0);
A.print_matrix();
}