-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.pas
219 lines (198 loc) · 4.21 KB
/
sudoku.pas
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
uses crt;
var
data: array [1..9, 1..9, 0..9] of integer;
row, col, square: array [0..9, 0..9] of boolean;
count, beforecount: integer;
function isrowexists(r, v: integer): boolean;
begin
isrowexists:=row[r, v];
end;
function iscolexists(c, v: integer): boolean;
begin
iscolexists:=col[c, v];
end;
function getVValue(r, c: integer): integer;
var vr, vc: integer;
begin
case r of
1..3: vr:=1;
4..6: vr:=2;
7..9: vr:=3;
end;
case c of
1..3: vc:=1;
4..6: vc:=2;
7..9: vc:=3;
end;
getVValue:=(vr-1)*3+vc;
end;
function issquareexists(r, c, v: integer): boolean;
begin
issquareexists:=square[getVValue(r, c), v];
end;
procedure initdata;
var r, c, i: integer;
begin
fillchar(row, sizeof(row), 0);
fillchar(col, sizeof(col), 0);
fillchar(square, sizeof(square), 0);
for r:=1 to 9 do
for c:=1 to 9 do
begin
row[r, data[r, c, 0]]:=true;
col[c, data[r, c, 0]]:=true;
square[getVValue(r, c), data[r, c, 0]]:=true;
end;
end;
procedure addcandidate(r, c, v: integer);
var i: integer;
begin
for i:=1 to 9 do
if (data[r, c, i]=0) or (data[r, c, i]=v) then
begin
data[r, c, i]:=v;
break;
end;
end;
procedure remcandidate(r, c, v: integer);
var i: integer;
fd: boolean;
begin
fd:=false;
for i:=1 to 8 do
if (fd) or (data[r, c, i]=v) then
begin
fd:=true;
data[r, c, i]:=data[r, c, i+1];
end;
if fd then
data[r, c, 9]:=0;
end;
procedure analyse;
var r, c, i: integer;
begin
for r:=1 to 9 do
begin
for c:=1 to 9 do
if (data[r, c, 0]=0) then
begin
for i:=1 to 9 do
if not ((row[r, i]) or (col[c, i]) or
(square[getVValue(r, c), i])) then
begin
addcandidate(r, c, i);
end
else
begin
remcandidate(r, c, i);
end;
end;
end;
end;
procedure solve;
var r, c, i: integer;
begin
beforecount:=count;
for r:=1 to 9 do
begin
for c:=1 to 9 do
if (data[r, c, 0]=0) then
begin
if (data[r, c, 2]=0) then
begin
count:=count-1;
data[r, c, 0]:=data[r, c, 1];
data[r, c, 1]:=0;
end;
end;
end;
end;
var
c, r, i: integer;
found: boolean;
begin
fillchar(data, sizeof(data), 0);
clrscr;
assign(input, 'sudo.txt');
assign(output, 'sudo.out');
rewrite(output);
reset(input);
count:=0;
for r:=1 to 9 do
begin
for c:=1 to 9 do
begin
read(data[r, c, 0]);
if (data[r, c, 0]=0) then
count:=count+1;
end;
readln;
end;
repeat
initdata;
{ for r:=1 to 9 do
begin
for c:=1 to 9 do
write(row[r, c], ' ');
writeln;
end;
writeln;
for r:=1 to 9 do
begin
for c:=1 to 9 do
write(col[r, c], ' ');
writeln;
end;
writeln;
for r:=1 to 9 do
begin
for c:=1 to 9 do
write(square[r, c], ' ');
writeln;
end;
writeln; }
analyse;
solve;
writeln('steps - count', count);
for r:=1 to 9 do
begin
for c:=1 to 9 do
write(data[r, c, 0], ' ');
writeln;
end;
writeln;
if (count=beforecount) then
begin
found:=false;
for r:=1 to 9 do
begin
for c:=1 to 9 do
if (data[r, c, 0]=0) then
begin
data[r, c, 0]:=data[r, c, 1];
remcandidate(r, c, data[r, c, 1]);
count:=count-1;
found:=true;
break;
end;
if found then break;
end;
end;
until (count<=0) or (count=beforecount);
if (count=beforecount) then
begin
writeln('unsolveable');
end;
for i:=0 to 9 do
begin
for r:=1 to 9 do
begin
for c:=1 to 9 do
write(data[r, c, i], ' ');
writeln;
end;
writeln;
end;
close(input);
close(output);
end.