操作
Problem 18¶
Maximum Path Sum I¶
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is $23$.
3
7 4
2 4 6
8 5 9 3
7 4
2 4 6
8 5 9 3
That is, $3 + 7 + 4 + 9 = 23$.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only $16384$ routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
数字の文字数¶
以下の三角形の頂点から下の行の隣接する数字を通って下まで移動するとき, その数値の和の最大値は23になる.
3
7 4
2 4 6
8 5 9 3
7 4
2 4 6
8 5 9 3
この例では 3 + 7 + 4 + 9 = 23.
以下の三角形を頂点から下まで移動するとき, その最大の和を求めよ.
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
注: ここではたかだか 16384 通りのルートしかないので, すべてのパターンを試すこともできる. Problem 67 は同じ問題だが100行あるので, 総当りでは解けない. もっと賢い方法が必要である.
#!r6rs
#!chezscheme
(import (chezscheme))
(define triangle
'#(#(75)
#(95 64)
#(17 47 82)
#(18 35 87 10)
#(20 04 82 47 65)
#(19 01 23 75 03 34)
#(88 02 77 73 07 63 67)
#(99 65 04 28 06 16 70 92)
#(41 41 26 56 83 40 80 70 33)
#(41 48 72 33 47 32 37 16 94 29)
#(53 71 44 65 25 43 91 52 97 51 14)
#(70 11 33 28 77 73 17 78 39 68 17 57)
#(91 71 52 38 17 14 91 43 58 50 27 29 48)
#(63 66 04 68 89 53 67 30 73 16 69 87 40 31)
#(04 62 98 27 23 09 70 98 73 93 38 53 60 04 23)))
(define (walkthrough triangle)
(let* ([triangle-length (vector-length triangle)]
[initial-result (let ([last-vector (vector-ref triangle (sub1 triangle-length))])
(map
(lambda (num)
`(,num ,num))
(vector->list last-vector)))])
(if (= triangle-length 1)
(cdar initial-result)
(let loop1 ([current-index (- triangle-length 2)]
[result initial-result])
(let* ([current-vector (vector-ref triangle current-index)]
[initial-list (vector->list current-vector)])
(if (zero? current-index)
(let* ([i-cell (car result)]
[j-cell (cadr result)]
[i-value (car i-cell)]
[j-value (car j-cell)]
[i-tree (cdr i-cell)]
[j-tree (cdr j-cell)]
[current-value (car initial-list)])
(if (<= j-value i-value)
(cons current-value i-tree)
(cons current-value j-tree)))
(let loop2 ([rest-nums initial-list]
[rest-tree result]
[temp-tree '()])
(if (null? rest-nums)
(loop1 (sub1 current-index) (reverse temp-tree))
(let* ([i-cell (car rest-tree)]
[j-cell (cadr rest-tree)]
[i-value (car i-cell)]
[j-value (car j-cell)]
[i-tree (cdr i-cell)]
[j-tree (cdr j-cell)]
[current-value (car rest-nums)])
(if (<= j-value i-value)
(loop2 (cdr rest-nums)
(cdr rest-tree)
(cons (cons (+ current-value i-value) (cons current-value i-tree))
temp-tree))
(loop2 (cdr rest-nums)
(cdr rest-tree)
(cons (cons (+ current-value j-value) (cons current-value j-tree))
temp-tree))))))))))))
(define answer-18
(apply + (walkthrough triangle)))
(printf "18: ~D~%" answer-18)
Noppi が2024/01/29に更新 · 7件の履歴