-
Notifications
You must be signed in to change notification settings - Fork 56
/
Sudoku2.cpp
219 lines (164 loc) · 4.32 KB
/
Sudoku2.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "stdio.h"
int InBlock[81], InRow[81], InCol[81];
const int BLANK = 0;
const int ONES = 0x3fe; // Binary 1111111110
int Entry[81]; // Records entries 1-9 in the grid, as the corresponding bit set to 1
int Block[9], Row[9], Col[9]; // Each int is a 9-bit array
int SeqPtr = 0;
int Sequence[81];
int Count = 0;
int LevelCount[81];
void SwapSeqEntries(int S1, int S2)
{
int temp = Sequence[S2];
Sequence[S2] = Sequence[S1];
Sequence[S1] = temp;
}
void InitEntry(int i, int j, int val)
{
int Square = 9 * i + j;
int valbit = 1 << val;
int SeqPtr2;
// add suitable checks for data consistency
Entry[Square] = valbit;
Block[InBlock[Square]] &= ~valbit;
Col[InCol[Square]] &= ~valbit; // Simpler Col[j] &= ~valbit;
Row[InRow[Square]] &= ~valbit; // Simpler Row[i] &= ~valbit;
SeqPtr2 = SeqPtr;
while (SeqPtr2 < 81 && Sequence[SeqPtr2] != Square)
SeqPtr2++ ;
SwapSeqEntries(SeqPtr, SeqPtr2);
SeqPtr++;
}
void PrintArray()
{
int i, j, valbit, val, Square;
char ch;
Square = 0;
for (i = 0; i < 9; i++) {
if (i % 3 == 0) putc('\n', stdout);
for (j = 0; j < 9; j++) {
if (j % 3 == 0) putc(' ', stdout);
valbit = Entry[Square++];
if (valbit == 0) ch = '-';
else {
for (val = 1; val <= 9; val++)
if (valbit == (1 << val)) {
ch = '0' + val;
break;
}
}
putc(ch,stdout);
}
putc ('\n', stdout);
}
}
void ConsoleInput()
{
int i, j;
char InputString[80];
for (i = 0; i < 9; i++) {
printf("Row[%d] : ", i + 1);
scanf("%s", InputString);
for (j = 0; j < 9; j++) {
char ch = InputString[j];
if (ch >= '1' && ch <= '9')
InitEntry(i, j, ch - '0');
}
}
PrintArray();
}
void PrintStats()
{
int i, j, S;
printf("\nLevel Counts:\n\n");
S = 0;
while (LevelCount[S] == 0) S++;
i = 0;
while (S < 81) {
int Seq = Sequence[S];
printf("(%d, %d):%4d ", Seq / 9 + 1, Seq % 9 + 1, LevelCount[S]);
if (i++ > 4){
printf("\n");
i = 0;
}
S++;
}
printf("\n\nCount = %d\n", Count);
}
void Succeed()
{
PrintArray();
PrintStats();
}
int NextSeq(int S)
{
int S2, Square, Possibles, BitCount;
int T, MinBitCount = 100;
for (T = S; T < 81; T++) {
Square = Sequence[T];
Possibles = Block[InBlock[Square]] & Row[InRow[Square]] & Col[InCol[Square]];
BitCount = 0;
while (Possibles) {
Possibles &= ~(Possibles & -Possibles);
BitCount++;
}
if (BitCount < MinBitCount) {
MinBitCount = BitCount;
S2 = T;
}
}
return S2;
}
void Place(int S)
{
LevelCount[S]++;
Count++;
if (S >= 81) {
Succeed();
return;
}
int S2 = NextSeq(S);
SwapSeqEntries(S, S2);
int Square = Sequence[S];
int BlockIndex = InBlock[Square],
RowIndex = InRow[Square],
ColIndex = InCol[Square];
int Possibles = Block[BlockIndex] & Row[RowIndex] & Col[ColIndex];
while (Possibles) {
int valbit = Possibles & (-Possibles); // Lowest 1 bit in Possibles
Possibles &= ~valbit;
Entry[Square] = valbit;
Block[BlockIndex] &= ~valbit;
Row[RowIndex] &= ~valbit;
Col[ColIndex] &= ~valbit;
Place(S + 1);
Entry[Square] = BLANK; // Could be moved out of the loop
Block[BlockIndex] |= valbit;
Row[RowIndex] |= valbit;
Col[ColIndex] |= valbit;
}
SwapSeqEntries(S, S2);
}
int main(int argc, char* argv[])
{
int i, j, Square;
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++) {
Square = 9 * i + j;
InRow[Square] = i;
InCol[Square] = j;
InBlock[Square] = (i / 3) * 3 + ( j / 3);
}
for (Square = 0; Square < 81; Square++) {
Sequence[Square] = Square;
Entry[Square] = BLANK;
LevelCount[Square] = 0;
}
for (i = 0; i < 9; i++)
Block[i] = Row[i] = Col[i] = ONES;
ConsoleInput();
Place(SeqPtr);
printf("\n\nTotal Count = %d\n", Count);
return 0;
}