Here is an update of the pjdowney program. Let me explain what it is about. It is a program that will find all solutions to a sudoku array with mathematical certainity. It will not miss any solutions and if no solution exists it will tell you. It is essentially 81 nested loops for the 81 cells in the array and anytime sudoku illegality is detected the rest of the inner loops are bypassed. The program has been changed to fix a few minor logic errors in the code ( the old program still worked 100% of the time) and display the array as the program works (fun to look at and only adds 1% to the overhead by displaying every 30,000 loops). It has been called a brute force utilitarian program and I agree. Attempts have been made to produce arrays that run longer with this program and you can by understanding what the program does but no attempt to make a single or multi-solution array that can't be solved has been successful. The program will always find all solutions time permitting. I hope this tool may be of assistance. /pjd
'Written by Paul J. Downey, Version 2
'pjdowney@peoplepc.com'this program will find all Sudoku solutions
'for the array entered in the data section
DEFINT A-Z
'defint makes the program alot faster
CLS : SL = 0
'SL is the solution number
DIM A(81), B(81, 2), C(81, 24), Z(81)
'A(81) is the main Sudoku array
'B(81,2) is the loops limit array. The program effectively
'runs 81 nexted for..next type loops using a variable pointer
'for example a variable loop pointer effrectively does
'FOR A(1)=B(1,1) TO B(1,2)
'FOR A(2)=B(2,1) TO B(2,2)...
'FOR A(81)=B(81,1) TO B(81,2)
'C(81,24) is the constraint array.
'C(n,1...8) are the other numbers in the same row as n
'C(n,9...16) are the other numbers in the same column as n
'C(n,17...24) are the other numbers in the same box as n
'where n is expressed as 1 to 81, the 9x9 in linear form.
'C(n,1...24) is then cleaned up to C(n,1...20) to avoid
'box duplication with rows and colums
'Z(n) tell you if location n has been given to you
'Z(n)=1 means location n was defined with a value in the
'sudoku puzzle else 0
FOR I = 1 TO 81: T = 0
FOR J = 1 TO 9
S = INT((I - 1) / 9) * 9 + J
IF S <> I THEN
T = T + 1
C(I, T) = S
END IF
NEXT J
T = 8
FOR J = 1 TO 9
S = I - INT((I - 1) / 9) * 9 + (J - 1) * 9
IF S <> I THEN
T = T + 1
C(I, T) = S
END IF
NEXT J
T = 16
I1 = INT(INT((I - 1) / 9) / 3)
J1 = INT((I - INT((I - 1) / 9) * 9 - 1) / 3)
FOR I2 = 0 TO 2
FOR J2 = 0 TO 2
S = I1 * 27 + I2 * 9 + J1 * 3 + J2 + 1
IF S <> I THEN
T = T + 1
C(I, T) = S
END IF
NEXT J2
NEXT I2
NEXT I
FOR I = 1 TO 81
T = 17
FOR J = 17 TO 24: L = 0
FOR K = 1 TO 16
IF C(I, J) = C(I, K) THEN L = 1
NEXT K
IF L = 0 THEN
C(I, T) = C(I, J)
T = T + 1
END IF
NEXT J
NEXT I
'all the above code does is make the constraint array
FOR I = 1 TO 81: PRINT "CELL #"; I: INPUT A(I)
IF A(I) = 0 THEN
B(I, 1) = 1
B(I, 2) = 9
Z(I) = 0
ELSE
B(I, 1) = A(I)
B(I, 2) = A(I)
Z(I) = 1
END IF
NEXT I
'the above code fills the upper and lower limits of the loops
'P is the variable pointer. The program is actually 81 nested
'loops with legality checks on each loop. The actual amount of
'calculation is actually very small to solve for every solution
'because immediately if non-suduku legality is detected the rest
'of the array looping is pruned.
'the next 30 lines or so is the actual solving routine, followed
'by a print routine
'I wrote this program in about an hour for quickbasic. If there
'is interest I can write it in a very generic form of basic that
'will run on anything. I think it will find all sudoku solutions
'quite quickly. People have e-mailed me that they are blown away
'by the search routine but couldn't figure out the code. I hope
'this helps.
TMR1& = TIMER: CLS : PC = 0
P = 1: A(1) = B(1, 1)
TOP: PC = PC + 1
IF PC = 30000 THEN
FOR I = 0 TO 8: FOR J = 1 TO 9: LOCATE I + 1, J * 3: PRINT A(I * 9 + J); : NEXT J: NEXT I
PC = 0
END IF
IF Z(P) = 1 THEN GOTO LGL
L = 1
FOR I = 1 TO 20
IF A(P) = A(C(P, I)) THEN
L = 0
EXIT FOR
END IF
NEXT I
IF L = 0 THEN GOTO NLGL
LGL: P = P + 1
IF P = 82 THEN
GOSUB PRTMAT
P = P - 1
GOTO NLGL
END IF
A(P) = B(P, 1)
GOTO TOP
NLGL: A(P) = A(P) + 1
IF A(P) <= B(P, 2) GOTO TOP
IF Z(P) = 1 THEN
A(P) = B(P, 1)
ELSE
A(P) = 0
END IF
P = P - 1
IF P = 0 THEN
PRINT "END OF SEARCH"
INPUT Z
STOP
END IF
GOTO NLGL
PRTMAT: SL = SL + 1
CLS
FOR P1 = 0 TO 8: FOR P2 = 1 TO 9
PRINT A(P1 * 9 + P2);
NEXT P2: PRINT : NEXT P1
PRINT "SOLUTION #=", SL
TMR2& = TIMER: PRINT "EXECUTION SECONDS= "; TMR2& - TMR1&
INPUT Y
CLS
RETURN