-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_cpp.c
210 lines (172 loc) · 5.08 KB
/
sudoku_cpp.c
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
203
204
205
206
207
208
209
210
/* Sudoku Breaker v0.2 by Skalgor */
#include <iostream>
#include <list>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#define DEBUG 0
#define LENGHT 9
#define CELLS LENGHT * LENGHT
#define BLOC_COLOR(i,j) i &= ~(1<<(j))
#define FREE_COLOR(i,j) i |= (1<<(j))
#define CHEK_COLOR(i,j) i & (1<<(j))
//Mapped access
short ROW[CELLS] = {0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8};
short COL[CELLS] = {0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8,
0,1,2,3,4,5,6,7,8};
short CHU[CELLS] = {0,0,0,1,1,1,2,2,2,
0,0,0,1,1,1,2,2,2,
0,0,0,1,1,1,2,2,2,
3,3,3,4,4,4,5,5,5,
3,3,3,4,4,4,5,5,5,
3,3,3,4,4,4,5,5,5,
6,6,6,7,7,7,8,8,8,
6,6,6,7,7,7,8,8,8,
6,6,6,7,7,7,8,8,8};
using namespace std;
class Node {
public:
short color;
short index;
short posible[9];
short fix[9];
Node()
{
color = 0;
index = 0;
}
};
class Graph {
public:
int solutions;
int recursion_level;
int depht;
int elegible_size;
int elegible_itr;
int row_block[LENGHT];
int col_block[LENGHT];
int chu_block[LENGHT];
int max_block[LENGHT];
Node * cell[CELLS];
Node * elegible_nodes[CELLS];
Graph()
{
solutions = 0;
recursion_level = 0;
depht = 0;
elegible_size = 0;
elegible_itr = 0;
cout << "Allocating memory..." << endl;
for(int i = 0; i < LENGHT; i++)
{
row_block[i] = -1;
col_block[i] = -1;
chu_block[i] = -1;
max_block[i] = 9;
}
for(int i = 0; i < CELLS; i++)
{
cell[i] = new Node();
cell[i]->color = 0;
cell[i]->index = i;
}
}
void read()
{
cout << "Reading graph..." << endl;
int r_color;
int index;
for(int i = 0; i < CELLS; i++)
{
cin >> r_color;
cell[i]->color = r_color;
index = cell[i]->index;
if (cell[i]->color != 0)
{
BLOC_COLOR(row_block[ROW[index]],r_color-1);
BLOC_COLOR(col_block[COL[index]],r_color-1);
BLOC_COLOR(chu_block[CHU[index]],r_color-1);
max_block[r_color-1]--;
depht++;
}
else
{
elegible_nodes[elegible_size++] = cell[i];
}
}
}
void print()
{
cout << "Printing graph..." << endl << endl;
for(int i = 0; i < CELLS; i++)
{
if ((i % 9)==0 && i!=0) cout << endl;
cout << cell[i]->color << " ";
}
cout << endl;
}
private:
};
void solve(Graph * sudo)
{
sudo->recursion_level++;
if(sudo->elegible_itr == sudo->elegible_size || sudo->depht == 81)
{
sudo->solutions++;
//sudo->print();
return;
}
Node * n = sudo->elegible_nodes[sudo->elegible_itr];
//TODO: Sacar ThisNode
for(int j = 1; j < 10; j++)
{
if( sudo->max_block[j-1] &&
CHEK_COLOR(sudo->row_block[ROW[n->index]],j-1) &&
CHEK_COLOR(sudo->col_block[COL[n->index]],j-1) &&
CHEK_COLOR(sudo->chu_block[CHU[n->index]],j-1) )
{
sudo->max_block[j-1]--;
BLOC_COLOR(sudo->row_block[ROW[n->index]],j-1);
BLOC_COLOR(sudo->col_block[COL[n->index]],j-1);
BLOC_COLOR(sudo->chu_block[CHU[n->index]],j-1);
n->color = j;
sudo->depht++;
sudo->elegible_itr++;
solve(sudo);
sudo->elegible_itr--;
sudo->depht--;
n->color = 0;
sudo->max_block[j-1]++;
FREE_COLOR(sudo->row_block[ROW[n->index]],j-1);
FREE_COLOR(sudo->col_block[COL[n->index]],j-1);
FREE_COLOR(sudo->chu_block[CHU[n->index]],j-1);
}
}
}
int main( )
{
std::cout << std::endl;
std::cout << "Inicializando..." << std::endl;
Graph * sudoku = new Graph();
sudoku->read();
solve(sudoku);
cout << "RC: " << sudoku->recursion_level;
cout << "SO: " << sudoku->solutions;
}