-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.f90
331 lines (268 loc) · 7.72 KB
/
utils.f90
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
module utils
use Stack
contains
subroutine SolveGrid(grid)
integer, dimension(9,9), intent(inout) :: grid
logical, dimension(9,9,9) :: possibs
logical :: changed
do while(any(grid.eq.0))
call PossibleEntries(grid,possibs)
call FindMove(grid,possibs,changed)
if (IsValidGrid(grid,possibs)) then
if (.not. changed) then
call TakeGuess(grid, possibs)
end if
else
call RetryGuess(grid)
end if
end do
!Clear rubbish out of the stack
Call EmptyStack()
end subroutine SolveGrid
recursive subroutine RetryGuess(grid)
integer, dimension(9,9), intent(inout) :: grid
integer, dimension(3) :: choice, nextchoice
logical, dimension(9,9,9) :: possibles
integer :: i,j,n, m
logical :: found
integer :: stat
!Ok our last guess failed so lets see if we can take another in that level
call PopGrid(grid,choice,stat)
if (stat .ne. 0) then !Cannot pop - just loop
write(*,*) 'stuck pop'
call writegrid(grid)
return
end if
i = choice(1)
j = choice(2)
n = choice(3)
!Recalculate possibles
call PossibleEntries(grid,possibles)
found = .false.
!Lets find the next choice
do m=n+1,9
if (possibles(j,i,m)) then
found = .true.
nextchoice = (/ i,j,m /)
end if
end do
if (.not. found) then
call RetryGuess(grid)
return
end if
call PushGrid(grid,nextchoice)
grid(j,i) = nextchoice(3)
end subroutine RetryGuess
subroutine TakeGuess(grid, possibles)
integer, dimension(9,9), intent(inout) :: grid
logical, dimension(9,9,9), intent(inout) :: possibles
integer, dimension(3) :: thischoice
integer :: minchoices, mini, minj
integer :: choices, i,j, n
mini = 0
minj = 0
choices = 0
minchoices = 100
!Find the item with the least possible choices
do i=1,9
do j=1,9
if (grid(j,i).ne.0) cycle
choices = count(possibles(j,i,:))
if (choices.lt.minchoices) then
minchoices = choices
mini = i
minj = j
end if
end do
end do
!Pick the first choice
do n=1,9
if (possibles(minj,mini,n)) then
choices = n
exit
end if
end do
!Sanity check
if (choices .eq. 0 .or. mini .eq. 0 .or. minj .eq. 0) then
call writegrid(grid)
write(*,*) mini, minj, choices, minchoices
STOP 'Something weird has happened'
end if
!Push the choice to the stack and do it
thischoice = (/ mini, minj, choices /)
call PushGrid(grid,thischoice)
grid(minj,mini) = choices
end subroutine TakeGuess
logical function IsValidGrid(grid, possibles)
integer, dimension(9,9) :: grid
logical, dimension(9,9,9), intent(in) :: possibles
integer :: ct
integer :: i,j,n
IsValidGrid = .true.
do n=1,9
!Check rows
do i=1,9
ct = count(grid(i,:) .eq. n)
if (ct .gt. 1) then
IsValidGrid = .false.
return
end if
!Cols...
ct = count(grid(:,i) .eq. n)
if (ct .gt. 1) then
IsValidGrid = .false.
return
end if
end do
!Lastly check if boxes only contain 1 of each number
do i=1,7,3
do j=1,7,3
ct = count(grid(j:j+2,i:i+2).eq.n)
if (ct .gt. 1) then
IsValidGrid = .false.
return
end if
end do
end do
end do
!Finally, check each grid point is either assigned or has choices
do i=1,9
do j=1,9
if (grid(j,i).eq.0) then
ct = count(possibles(j,i,:))
if (ct.lt.1) then
IsValidGrid = .false.
return
end if
end if
end do
end do
end function IsValidGrid
subroutine PossibleEntries(grid,possibles)
integer, dimension(9,9), intent(in) :: grid
logical, dimension(9,9,9), intent(inout) :: possibles
integer :: i,j, squarex, squarey, p,q
possibles = .true.
do i=1,9
do j=1,9
if (grid(j,i).eq.0) cycle
!Already have this one and it is fixed
possibles(j,i,:) = .false.
!Not zero so remove this number from every item in row, column and box
possibles(:,i,grid(j,i)) = .false.
possibles(j,:,grid(j,i)) = .false.
squarex = ((i-1)/3) * 3 + 1
squarey = ((j-1)/3) * 3 + 1
do p=squarex,squarex+2
do q=squarey,squarey+2
possibles(q,p,grid(j,i)) = .false.
end do
end do
end do
end do
end subroutine PossibleEntries
subroutine FindMove(grid,possibles,changed)
integer, dimension(9,9), intent(inout) :: grid
logical, dimension(9,9,9), intent(in) :: possibles
logical, intent(out) :: changed
integer :: i, n, j, p,q
changed = .false.
!First see if any squares only have one choice anywhere
do i=1,9
do j=1,9
if (count(possibles(j,i,:)).eq.1) then
do n=1,9
if (possibles(j,i,n)) then
grid(j,i) = n
changed = .true.
exit
end if
end do
end if
end do
end do
!Look over numbers
do n=1,9
!Loop columns
do i=1,9
if (count(possibles(:,i,n)).eq.1) then
!Find the move
do j=1,9
if (possibles(j,i,n)) then
grid(j,i) = n
changed = .true.
exit
end if
end do
end if
end do
!...rows
do i=1,9
if (count(possibles(i,:,n)).eq.1) then
!Find the move
do j=1,9
if (possibles(i,j,n)) then
grid(i,j) = n
changed = .true.
exit
end if
end do
end if
end do
!Finally count all in this square...
do i=1,7,3
inner: do j=1,7,3
!Lets see if this square contains the number
if (count(possibles(j:j+2,i:i+2,n)).eq.1) then
!look over these
do p=i,i+2
do q=j,j+2
if (possibles(q,p,n)) then
grid(q,p) = n
cycle inner
end if
end do
end do
end if
end do inner
end do
end do
end subroutine FindMove
subroutine ReadGrid(filename,grid, nGrids)
character(len=*), intent(in) :: filename
integer, dimension(9,9,nGrids), intent(out) :: grid
integer, intent(in) :: nGrids
character(len=5) :: stuff
integer :: gridno, i,j
open(unit=10,file=trim(filename))
do j=1,nGrids
read(10,'(A5,I2)') stuff, gridno
!Read 9 lines
do i=1,9
read(10,'(9I1)') grid(:,i,j)
end do
end do
close(unit=10)
end subroutine ReadGrid
subroutine ReadGrid2(filename,grid, nGrids)
character(len=*), intent(in) :: filename
integer, dimension(9,9,nGrids), intent(out) :: grid
integer, intent(in) :: nGrids
integer :: j
open(unit=10,file=trim(filename))
do j=1,nGrids
read(10,'(81I1)') grid(:,:,j)
end do
close(unit=10)
end subroutine ReadGrid2
subroutine WriteGrid(grid)
integer, dimension(9,9), intent(inout) :: grid
integer :: i
write(*,*) '-----------'
900 format (3I1,' ',3I1,' ', 3I1)
do i=1,9
write(*,900) grid(:,i)
if (mod(i,3).eq.0) write(*,*)
end do
end subroutine WriteGrid
end module utils