Problem 81 "Path sum: two ways"

In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to 2427.

$$ \begin{pmatrix} \color{red}{131} & 673 & 234 & 103 & 18\\ \color{red}{201} & \color{red}{96} & \color{red}{342} & 965 & 150\\ 630 & 803 & \color{red}{746} & \color{red}{422} & 111\\ 537 & 699 & 497 & \color{red}{121} & 956\\ 805 & 732 & 524 & \color{red}{37} & \color{red}{331} \end{pmatrix} $$

Find the minimal path sum from the top left to the bottom right by only moving right and down in matrix.txt, a 31K text file containing an 80 by 80 matrix.

問 81 「経路の和:2方向」

下記の5次の正方行列で, 左上のセルから開始し右下のセルで終わるパスを探索する.

ただし下方向と右方向にのみ移動できるものとする.

通過したセルの和が最小となるパスは赤の太字で示されたもので, その値は2427である.

今, 31Kのテキストファイmatrix.txtには80×80の行列が書かれている.

同様に左上のセルから開始し右下のセルで終わり, かつ右方向と下方向にのみ移動するときの最小のパスの和を求めよ.

Since the dataset of the question is too large, it's using a mini set.

fn main() {
    let mut table = vec![
        vec![131, 673, 234, 103, 18],
        vec![201, 96, 342, 965, 150],
        vec![630, 803, 746, 422, 111],
        vec![537, 699, 497, 121, 956],
        vec![805, 732, 524, 37, 331]
    ];
    for y in 0..table.len() {
        for x in 0..table[0].len() {
            table[y][x] += match (y, x) {
                (0, 0) => continue,
                (0, _) => table[y][x - 1],
                (_, 0) => table[y - 1][x],
                _ => std::cmp::min(table[y][x - 1], table[y - 1][x]),
            }
        }
    }
    let min = table[table.len() - 1][table[table.len() - 1].len() - 1];

    println!("{}", min);
    assert_eq!(min, 2427);
}