DEFINT A-Z
CLS : IP = 0
DIM A(81), B(81, 2), C(81, 24), Z(81)
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: READ 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
P = 1: A(1) = B(1, 1)
TOP: L = 1: GOSUB PRTMAT
FOR I = 1 TO 24
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
IP = 999
GOSUB PRTMAT
PRINT "SOLUTION"
INPUT Z
STOP
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
GOTO NLGL
PRTMAT: IP = IP + 1
IF IP = 1000 THEN
CLS
FOR P1 = 0 TO 8: FOR P2 = 1 TO 9
PRINT A(P1 * 9 + P2);
NEXT P2: PRINT : NEXT P1
IP = 0
END IF
RETURN
DATA 1,0,0,2,8,7,0,0,0
DATA 0,0,0,0,0,0,0,0,3
DATA 9,0,0,6,3,0,0,0,1
DATA 0,0,0,0,7,0,0,0,6
DATA 7,9,5,0,6,1,2,0,0
DATA 8,3,0,5,0,0,0,0,0
DATA 3,0,0,0,0,0,0,0,0
DATA 0,0,7,0,0,9,0,0,5
DATA 2,0,1,7,0,0,6,0,4
Folks, program works good. Just put the initial puzzle in the data area.
The program was written in quick basic 4.5 in about an hour and a half.
The first portion creates the constraint array and the second part uses a variable pointer to simulate 81 nested loops. It solves on my hyper threading machine in about .02 seconds any puzzle. I would be happy to provide tech assistance. I have a background in physics and cryptography. I hope it does not take the fun out of it. /PJD