Fill up the the 20 empty boxes in the figure shown below, using the 10 base ten digits 0,1,2,3,4,5,6,7,8,9 each occurring exactly twice such that:
(a) Each horizontal number is a multiple of 7.
(b) Each vertical number is a multiple of 7.
(c) The total sum of all the horizontal and vertical numbers is the maximum of all such arrangements satisfying both (a) and (b).
+---+
| |
+---+---+---+
| | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | |
+---+---+ +---+---+
Note: As the figure shows, the bottom row should be considered as two 2-digit numbers rather than a single 4-digit number.
DECLARE SUB addOn (nbr#, offset#)
DEFDBL A-Z
CLEAR , , 25000
OPEN "7 mult muse.txt" FOR OUTPUT AS #2
DIM SHARED gr(5, 7), begend(13, 4), avail AS STRING, maxnbr, overtot, maxovertot
DATA 1,4,1,4
DATA 2,3,2,5
DATA 1,4,4,4
DATA 3,2,3,6
DATA 2,3,4,3
DATA 2,5,4,5
DATA 4,1,4,7
DATA 3,2,5,2
DATA 3,6,5,6
DATA 4,1,5,1
DATA 4,7,5,7
DATA 5,1,5,2
DATA 5,6,5,7
FOR i = 1 TO 13
READ r0, c0, r1, c1
IF i > 1 THEN
begend(i, 1) = r0
begend(i, 2) = c0
begend(i, 3) = r1
begend(i, 4) = c1
dr = SGN(r1 - r0): dc = SGN(c1 - c0)
r = r0: c = c0
WHILE r <= r1 AND c <= c1
gr(r, c) = -1
r = r + dr: c = c + dc
WEND
END IF
NEXT
avail = "0011223344556678899"
overtot = 7
gr(1, 4) = 7
addOn 2, 0
close
SUB addOn (nbr, offset)
r0 = begend(nbr, 1)
c0 = begend(nbr, 2)
r1 = begend(nbr, 3)
c1 = begend(nbr, 4)
dr = SGN(r1 - r0): dc = SGN(c1 - c0)
r = r0 + dr * offset: c = c0 + dc * offset
IF gr(r, c) = -1 THEN
IF offset = 0 THEN st = 1: ELSE st = 0
FOR v = st TO 9
vchar$ = LTRIM$(STR$(v))
ix = INSTR(avail, vchar$)
IF ix > 0 THEN
MID$(avail, ix, 1) = " "
gr(r, c) = v
IF r = r1 AND c = c1 THEN
tot = 0
rx = r0: cx = c0
WHILE rx <= r1 AND cx <= c1
tot = 10 * tot + gr(rx, cx)
rx = rx + dr: cx = cx + dc
WEND
IF tot MOD 7 = 0 THEN
overtot = overtot + tot
IF nbr = 13 THEN
IF overtot >= maxovertot THEN
maxovertot = overtot
FOR rx = 1 TO 5
FOR cx = 1 TO 7
PRINT USING "##"; gr(rx, cx);
PRINT #2, USING "##"; gr(rx, cx);
NEXT
PRINT: PRINT #2,
NEXT
PRINT overtot
PRINT #2, overtot
END IF
ELSE
addOn nbr + 1, 0
END IF
overtot = overtot - tot
END IF
ELSE
addOn nbr, offset + 1
END IF
gr(r, c) = -1
MID$(avail, ix, 1) = vchar$
END IF
NEXT
ELSE
IF r = r1 AND c = c1 THEN
tot = 0
rx = r0: cx = c0
WHILE rx <= r1 AND cx <= c1
tot = 10 * tot + gr(rx, cx)
rx = rx + dr: cx = cx + dc
WEND
IF tot MOD 7 = 0 THEN
overtot = overtot + tot
IF nbr = 13 THEN
IF overtot >= maxovertot THEN
maxovertot = overtot
FOR rx = 1 TO 5
FOR cx = 1 TO 7
PRINT USING "##"; gr(rx, cx);
PRINT #2, USING "##"; gr(rx, cx);
NEXT
PRINT: PRINT #2,
NEXT
PRINT overtot
PRINT #2, overtot
END IF
ELSE
addOn nbr + 1, 0
END IF
overtot = overtot - tot
END IF
ELSE
addOn nbr, offset + 1
END IF
END IF
END SUB
finds as the last two entries (with therefore the largest total), these, with a grand total of 10,019,520 each (I've manually blanked out the extraneous zeros printed from grid locations not in the puzzle area):
7
3 4 3
2 7 6 0 1
9 9 8 2 8 0 5
1 4 5 6
10019520
7
3 4 3
2 7 6 0 8
9 9 8 2 1 0 5
1 4 5 6
10019520
|
Posted by Charlie
on 2013-12-23 23:28:37 |