Numerically
7 3 8 20 6
19 1 24 4 11
10 21 9 17 15
26 5 25 2 22
16 23 13 18 14
and as letters
GCHTF
SAXDK
JUIQO
ZEYBV
PWMRN
The program didn't take into consideration the non-adjacency, which apparently wasn't needed as the above solution was the only one found, even without this restriction; but it does meet that restriction, so it was more of a hint than a requirement. Numeric values are kept throughout until a letter translation is given of the solution.
The program was speeded by the fact that only the top row is arbitrary (another row or column could have been chosen, but I chose this). Each entry in subsequent rows is dictated by the row before so as to make the sum correct in the grid position immediately above, taking into consideration the other 1, 2 or 3 numbers around it already assigned. If a number that's already used is required, then that whole sequence of moves dependent on row 1 is invalid.
DECLARE SUB place (row!, col!)
CLEAR , , 9999
DIM SHARED board(6, 6)
DIM SHARED tots(5, 5)
DIM SHARED taken(26)
taken(12) = 1
DATA 22,16,47,18,31
DATA 18,67,22,72,25
DATA 66,25,87,30,50
DATA 31,95,29,82,31
DATA 49,34,66,29,40
FOR r = 1 TO 5
FOR c = 1 TO 5
READ tots(r, c)
NEXT
NEXT
place 1, 1
SUB place (row, col)
IF row = 2 AND col = 3 THEN
IF board(1, 3) + board(2, 2) > 22 THEN EXIT SUB
board(row, col) = 24
c = col + 1: r = row
IF c > 5 THEN r = r + 1: c = 1
place r, c
EXIT SUB
END IF
IF row < 5 AND col < 5 THEN
IF board(row - 1, col) + board(row, col - 1) > tots(row, col) - 3 THEN EXIT SUB
END IF
IF row > 1 THEN
v = tots(row - 1, col) - board(row - 2, col) - board(row - 1, col - 1) - board(row - 1, col + 1)
IF v = 24 OR v < 1 OR v > 26 THEN EXIT SUB
IF taken(v) = 0 THEN
taken(v) = 1
board(row, col) = v
IF row = 5 AND col = 5 THEN
FOR r = 1 TO 5
FOR c = 1 TO 5
PRINT USING "## "; board(r, c);
NEXT: PRINT
NEXT: PRINT
FOR r = 1 TO 5
FOR c = 1 TO 5
PRINT MID$("ABCDEFGHIJKLMNOPQRSTUVWXYZ", board(r, c), 1);
NEXT: PRINT
NEXT: PRINT
ELSE
c = col + 1: r = row
IF c > 5 THEN r = r + 1: c = 1
place r, c
END IF
board(row, col) = 0
taken(v) = 0
ELSE
EXIT SUB
END IF
ELSE
FOR v = 1 TO 26
IF col = 1 THEN PRINT col, v
IF v <> 24 AND taken(v) = 0 THEN
board(row, col) = v
taken(v) = 1
c = col + 1: r = row
IF c > 5 THEN r = r + 1: c = 1
place r, c
taken(v) = 0
board(row, col) = 0
END IF
NEXT
END IF
END SUB
|
Posted by Charlie
on 2007-05-03 09:46:01 |