+---+ +---+ +---+ +---+ +---+
| 4 |-->| ● |-->| 9 |-->| ● |-->| 7 |
+---+ +---+ +---+ +---+ +---+
^ ^ ^ ^ ^
| | | | |
+---+ +---+ +---+ +---+ +---+
| ● |-->| 7 |-->| ● |-->| 5 |-->| ● |
+---+ +---+ +---+ +---+ +---+
^ ^ ^ ^ ^
| | | | |
+---+ +---+ +---+ +---+ +---+
| 5 |-->| ● |-->| 9 |-->| ● |-->| 4 |
+---+ +---+ +---+ +---+ +---+
^ ^ ^ ^ ^
| | | | |
+---+ +---+ +---+ +---+ +---+
| ● |-->| 8 |-->| ● |-->| 6 |-->| ● |
+---+ +---+ +---+ +---+ +---+
^ ^ ^ ^ ^
| | | | |
+---+ +---+ +---+ +---+ +---+
| 6 |-->| ● |-->| 8 |-->| ● |-->| 5 |
+---+ +---+ +---+ +---+ +---+
Move from the bottom left-hand 6 to the top right-hand 7 in the direction of the arrows, adding together all the five numbers .
Each black circle is worth minus 5 and this will be taken from the total each time you meet one.
How many different routes are there- each giving a total of 10 ?
DECLARE SUB moveIt ()
CLEAR , , 25000
CLS
DIM SHARED gr(5, 5), totWays, row, col, totval, lv, hist(10)
OPEN "diffrtes.txt" FOR OUTPUT AS #2
DATA 4,-5,9,-5,7
DATA -5,7,-5,5,-5
DATA 5,-5,9,-5,4
DATA -5,8,-5,6,-5
DATA 6,-5,8,-5,5
FOR row = 1 TO 5
FOR col = 1 TO 5
READ gr(row, col)
NEXT
NEXT
row = 5: col = 1
totval = gr(row, col)
lv = 1: hist(1) = 6
moveIt
PRINT totWays
SUB moveIt
lv = lv + 1
IF row = 1 AND col = 5 THEN
IF totval = 10 THEN
totWays = totWays + 1
FOR i = 1 TO lv - 1: PRINT hist(i); : NEXT: PRINT
FOR i = 1 TO lv - 1
IF hist(i) <> -5 THEN PRINT #2, hist(i);
NEXT: PRINT #2,
END IF
ELSE
FOR delcol = 0 TO 1
delrow = delcol - 1
IF col + delcol < 6 AND delcol > 0 THEN
col = col + delcol
totval = totval + gr(row, col)
hist(lv) = gr(row, col)
moveIt
totval = totval - gr(row, col)
col = col - delcol
END IF
IF row + delrow > 0 AND delrow < 0 THEN
row = row + delrow
totval = totval + gr(row, col)
hist(lv) = gr(row, col)
moveIt
totval = totval - gr(row, col)
row = row - delrow
END IF
NEXT
END IF
lv = lv - 1
END SUB
finds five routes:
6 5 7 5 7
6 5 7 5 7
6 5 7 5 7
6 5 7 5 7
6 8 5 4 7
The numbers add up to 30, so the four dots each incurs along the way subtract a total of 20, leaving 10.
The sequence 6, 5, 7, 5, 7 occurs four times as there are two ways of getting from each 5 to each 7. The sole way of getting 6, 8, 5, 4, 7 is along the bottom and then up the right side.
|
Posted by Charlie
on 2013-09-09 12:11:38 |