-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku0.rb
279 lines (248 loc) · 7.57 KB
/
sudoku0.rb
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
$maxLoop = ARGV[1].to_i
$analogy = 0
class Sudoku < Array
def initialize(filename)
f = File.open(filename, mode = "rt")
sudoku = []
10.times do |i|
sudokuEach = []
9.times do
line = []
3.times do
vector = []
3.times do
if i == 0
vector.push(f.getc.to_i)
else
if f.getc.to_i == i
vector.push(i)
else
vector.push(0)
end
end
end
line.push(vector)
end
sudokuEach.push(line)
f.getc #改行送り
end
sudoku.push(sudokuEach)
f.rewind
end
@sudoku = sudoku
f.close
end
def show(i)
@sudoku[i].each do |line|
line.each do |vector|
vector.each do |num|
if num == 0
print " "
else
print num
end
end
end
puts ""
end
end
def result
@sudoku[0].flatten.join
end
def hSolve
flag = false
9.times do |num|
sudoku = @sudoku[num+1]
9.times do |i|
#puts sudoku[i].flatten.count(0)
if sudoku[i].flatten.count(0) == 1
3.times do |j|
3.times do |k|
if sudoku[i][j][k] == 0
@sudoku[0][i][j][k] = sudoku[i][j][k] = num+1
flag = true
printf("Horisontaly solve!:(%d,%d)->%d\n",i+1,j*3+k+1,num+1)
end
end
end
end
end
self[num+1] = sudoku
self.fill
end
return flag
end
def vSolve
flag = false
9.times do |num|
sudoku = @sudoku[num+1]
3.times do |i|
3.times do |j|
marker = []
9.times do |k|
if sudoku[k][i][j] == 0
marker.push([k,i,j])
end
end
#p marker.count
if marker.count == 1
#p marker
@sudoku[0][marker[0][0]][marker[0][1]][marker[0][2]] = sudoku[marker[0][0]][marker[0][1]][marker[0][2]] = num+1
flag = true
printf("Verticaly solve!:(%d,%d)->%d\n",marker[0][0]+1,marker[0][1]*3+marker[0][2]+1,num+1)
end
end
end
self[num+1] = sudoku
self.fill
end
return flag
end
def bSolve
flag = false
9.times do |num|
sudoku = @sudoku[num+1]
3.times do |i|
3.times do |j|
marker = []
3.times do |k|
3.times do |l|
if sudoku[i*3+k][j][l] == 0
marker.push([i*3+k,j,l])
end
end
end
if marker.count == 1
@sudoku[0][marker[0][0]][marker[0][1]][marker[0][2]] = sudoku[marker[0][0]][marker[0][1]][marker[0][2]] = num+1
flag = true
printf("Box solve!:(%d,%d)->%d\n",marker[0][0]+1,marker[0][1]*3+marker[0][2]+1,num+1)
end
end
end
self[num+1] = sudoku
self.fill
end
return flag
end
def fill
9.times do |num|
#puts num+1
hFlag = Array.new(9, false)
vFlag = Array.new(9, false)
bFlag = Array.new(9, false)
sudoku = []
9.times do |i|
line = []
3.times do |j|
line[j] = [0,0,0]
end
sudoku[i] = line
end
9.times do |line|
3.times do |vector|
3.times do |value|
if @sudoku[num+1][line][vector][value] == num+1
hFlag[line] = true
vFlag[vector*3 + value] = true
bFlag[line/3*3 + vector] = true
sudoku[line][vector][value] = num+1
end
if @sudoku[0][line][vector][value] != 0 && @sudoku[0][line][vector][value] != num+1
sudoku[line][vector][value] = "+"
end
end
end
end
9.times do |i|
if hFlag[i]
3.times do |j|
3.times do |k|
if sudoku[i][j][k] == 0
sudoku[i][j][k] = "+"
end
end
end
end
end
9.times do |i|
if vFlag[i]
9.times do |j|
#printf("%d -> 1 (%d, %d)\n",sudoku[j][i/3][i%3],j,i/3+i%3)
#p sudoku[j]
if sudoku[j][i/3][i%3] == 0
sudoku[j][i/3][i%3] = "+"
end
#p sudoku[j]
#p "----------"
end
end
end
#p bFlag
9.times do |i|
if bFlag[i]
3.times do |j|
#p sudoku[i/3+j][i%3]
3.times do |k|
if sudoku[i/3*3+j][i%3][k] == 0
sudoku[i/3*3+j][i%3][k] = "+"
end
end
#printf("%d,%d\n",i/3+j,i%
end
#p sudoku[i/3+j][i%3].object_id
end
end
@sudoku[num+1] = sudoku
end
end
def fin?
return !@sudoku[0].flatten.include?(0)
end
def copy
Marshal.load(Marshal.dump(self))
end
end
def showall(sudoku)
puts "---------"
10.times do |i|
sudoku.show(i)
puts "---------"
end
end
def solve(sudoku)
ARGV[1].to_i.times do |loop|
flag = false
hFlag = sudoku.hSolve
vFlag = sudoku.vSolve
bFlag = sudoku.bSolve
flag = hFlag || vFlag || bFlag
puts "\n"
if !flag
return
end
sudoku.show(0)
if ARGV[2].to_i != 0
puts "---------"
sudoku.show(ARGV[2].to_i)
end
printf("\nLOOP%d=============================\n\n",loop+1)
end
end
def main
if ARGV[1].to_i == 0
ARGV[1] = "100"
end
sudoku = Sudoku.new(ARGV[0])
sudoku.fill
showall(sudoku)
printf("\nDisplay Type:%d\nMax Loop:%d\n\nFin init======================\n\n",ARGV[2].to_i,ARGV[1].to_i)
ARGV[1].to_i.times do
solve(sudoku)
if sudoku.fin?
printf("\nfin!\n%s\n\n",sudoku.result)
sudoku.show(0)
return
end
end
end
main