提出 #14018954


ソースコード 拡げる

fn main() {
    let (h, w): (usize, usize) = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let mut iter = buf.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    };

    let mut a: Vec<Vec<usize>> = Vec::with_capacity(h);

    for _ in 0..h {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let iter = buf.split_whitespace();
        a.push(iter.map(|x| x.parse().unwrap()).collect());
    }

    for i in 0..h {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let iter = buf.split_whitespace();
        for (j, x) in iter.map(|x| x.parse::<i32>().unwrap()).enumerate() {
            a[i][j] = (a[i][j] as i32 - x).abs() as usize;
        }
    }

    let m: usize = (h + w) * 80;

    let mut dp = vec![vec![vec![false; m]; w]; h];
    dp[0][0][a[0][0]] = true;

    for i in 0..h {
        for j in 0..w {
            for k in 0..m {
                if dp[i][j][k] == false { continue; }

                if i + 1 < h {
                    dp[i + 1][j][k + a[i + 1][j]] = true;
                    dp[i + 1][j][(k as i32 - a[i + 1][j] as i32).abs() as usize] = true;
                    dp[i + 1][j][(a[i + 1][j] as i32 - k as i32).abs() as usize] = true;
                }

                if j + 1 < w {
                    dp[i][j + 1][k + a[i][j + 1]] = true;
                    dp[i][j + 1][(k as i32 - a[i][j + 1] as i32).abs() as usize] = true;
                    dp[i][j + 1][(a[i][j + 1] as i32 - k as i32).abs() as usize] = true;
                }
            }
        }
    }

    let mut ans = std::usize::MAX;
    for (i, &x) in dp[h - 1][w - 1].iter().enumerate() {
        if x == true {
            ans = std::cmp::min(ans, i);
        }
    }

    println!("{}", ans);
}

提出情報

提出日時
問題 E - Balanced Path
ユーザ Strorkis
言語 Rust (1.15.1)
得点 500
コード長 2008 Byte
結果 AC
実行時間 435 ms
メモリ 96508 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 2
AC × 25
セット名 テストケース
Sample sample_01, sample_02
All hand_01, hand_02, hand_03, hand_04, hand_05, hand_06, max_01, max_02, max_03, max_04, max_05, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, random_11, random_12, sample_01, sample_02
ケース名 結果 実行時間 メモリ
hand_01 AC 168 ms 96508 KiB
hand_02 AC 169 ms 96508 KiB
hand_03 AC 168 ms 96508 KiB
hand_04 AC 169 ms 96508 KiB
hand_05 AC 169 ms 96508 KiB
hand_06 AC 168 ms 96508 KiB
max_01 AC 429 ms 94460 KiB
max_02 AC 184 ms 94460 KiB
max_03 AC 181 ms 94460 KiB
max_04 AC 167 ms 96508 KiB
max_05 AC 175 ms 96508 KiB
random_01 AC 156 ms 37116 KiB
random_02 AC 154 ms 35068 KiB
random_03 AC 191 ms 41212 KiB
random_04 AC 166 ms 37116 KiB
random_05 AC 75 ms 18684 KiB
random_06 AC 435 ms 96508 KiB
random_07 AC 434 ms 96508 KiB
random_08 AC 434 ms 96508 KiB
random_09 AC 434 ms 96508 KiB
random_10 AC 434 ms 96508 KiB
random_11 AC 335 ms 94460 KiB
random_12 AC 349 ms 94460 KiB
sample_01 AC 2 ms 4352 KiB
sample_02 AC 2 ms 4352 KiB