A B C D E
+-----+-----+-----+-----+-----+
| | | | | |
| 1 | 7 | 13 | 20 | 6 |
+-----+-----+-----+-----+-----+
| | | | | |
| 25 | 9 | 2 | 23 | 11 |
+-----+-----+-----+-----+-----+
| | | | | |
| 14 | 22 | 17 | 8 | 16 |
+-----+-----+-----+-----+-----+
| | | | | |
| 4 | 12 | 10 | 3 | 19 |
+-----+-----+-----+-----+-----+
| | | | | |
| 24 | 18 | 5 | 15 | 21 |
+-----+-----+-----+-----+-----+
Add together five different numbers from the grid above to achieve the highest possible total. Starting at column A and working your way across to column E, only one number may be chosen from each column, and no two
numbers may be from the same row or diagonal line.
DECLARE SUB addOn (col!)
CLEAR , , 25000
DIM SHARED grid(5, 5), h(5), tot, max, hmax(5), ct
CLS
DATA 1,7,13,20,6
DATA 25,9,2,23,11
DATA 14,22,17,8,16
DATA 4,12,10,3,19
DATA 24,18,5,15,21
FOR row = 1 TO 5
FOR col = 1 TO 5
READ grid(row, col)
NEXT
NEXT
FOR r = 1 TO 5
h(1) = r
tot = grid(r, 1)
addOn 2
NEXT
PRINT
PRINT ct
PRINT max
FOR c = 1 TO 5
PRINT hmax(c);
NEXT: PRINT
FOR c = 1 TO 5
PRINT grid(hmax(c), c);
NEXT: PRINT
SUB addOn (col)
FOR r = 1 TO 5
good = 1
FOR c = 1 TO col - 1
IF r = h(c) THEN good = 0: EXIT FOR
IF col - c = ABS(r - h(c)) THEN good = 0: EXIT FOR
NEXT
IF good THEN
h(col) = r
tot = tot + grid(r, col)
IF col = 5 THEN
ct = ct + 1
FOR c = 1 TO 5
PRINT grid(h(c), c);
NEXT c: PRINT , tot
IF tot > max THEN
FOR c = 1 TO 5
hmax(c) = h(c)
NEXT
max = tot
END IF
ELSE
addOn (col + 1)
END IF
tot = tot - grid(r, col)
END IF
NEXT
END SUB
finds only ten possible choices whose totals need to be compared:
Numbers total
1 22 5 23 19 70
1 12 2 15 16 46
25 12 13 8 21 79
25 18 17 20 19 99
14 7 10 23 21 75
14 18 2 3 6 43
4 7 17 15 11 54
4 9 5 8 6 32
24 9 10 20 16 79
24 22 13 3 11 73
So the highest total is 99.
The row choice for each column, with its value listed below each, are:
2 5 3 1 4
25 18 17 20 19
|
Posted by Charlie
on 2013-05-21 17:15:21 |