結果

問題 No.611 Day of the Mountain
ユーザー lzy9lzy9
提出日時 2018-04-07 10:45:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 319 ms / 2,017 ms
コード長 2,676 bytes
コンパイル時間 4,642 ms
コンパイル使用メモリ 161,072 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-08 18:11:47
合計ジャッジ時間 7,152 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 299 ms
4,380 KB
testcase_01 AC 296 ms
4,376 KB
testcase_02 AC 296 ms
4,380 KB
testcase_03 AC 319 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 0 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::rc::Rc`
 --> Main.rs:4:5
  |
4 | use std::rc::Rc;
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: static `DX` is never used
  --> Main.rs:19:8
   |
19 | static DX: &'static [i32] = &[0, 0, 1, -1];
   |        ^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: static `DY` is never used
  --> Main.rs:20:8
   |
20 | static DY: &'static [i32] = &[1, -1, 0, 0];
   |        ^^

warning: 3 warnings emitted

ソースコード

diff #

use std::io::*;
use std::str::FromStr;
use std::cmp::{min, max};
use std::rc::Rc;
use std::mem::swap;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin_lock = stdin.lock();
    let s = stdin_lock
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok().unwrap()
}

static DX: &'static [i32] = &[0, 0, 1, -1];
static DY: &'static [i32] = &[1, -1, 0, 0];

fn main() {
    let mut h: usize = read();
    let mut w: usize = read();
    let input: Vec<Vec<i32>> = (0..h).map(|_| read::<String>().chars().map(|c| if c == '?' { -1 } else { c.to_digit(10).unwrap() as i32 }).collect()).collect();
    let map = if h < w {
        let mut tmp = vec![vec![0 as i32; h]; w];
        for i in 0..h {
            for j in 0..w {
                tmp[j][i] = input[i][j];
            }
        }
        swap(&mut h, &mut w);
        tmp
    } else {
        input
    };

    let mut dist = vec![vec![1010101010; w]; h];

    dist[0][0] = 0;

    for i in 0..h {
        for j in 0..w {
            if i != 0 {
                dist[i][j] = min(dist[i][j], dist[i - 1][j]);
            }

            if j != 0 {
                dist[i][j] = min(dist[i][j], dist[i][j - 1]);
            }
            dist[i][j] += max(1, map[i][j]);
        }
    }

    println!("{}", dist[h - 1][w - 1]);

    let mut dp = vec![0 as u64; 1 << w];

    dp[1] = 1;
    let m = 201712111u64;

    for i in 0..h {
        for j in 0..w {
            let mut dp2 = vec![0 as u64; 1 << w];

            for k in 0..(1 << w) {
                let mut f = false;
                let c = if map[i][j] == -1 { 1 } else { map[i][j] };
                if (k >> j) & 1 == 1 && (i == 0 || dist[i - 1][j] + c == dist[i][j]) {
                    f = true;
                }

                if j != 0 && (k >> (j - 1)) & 1 == 1 && dist[i][j - 1] + c == dist[i][j] {
                    f = true;
                }

                if f {
                    dp2[k | (1 << j)] += dp[k];
                    dp2[k | (1 << j)] %= m;

                    if map[i][j] == -1 {
                        dp2[k & !(1 << j)] += dp[k] * 8;
                        dp2[k & !(1 << j)] %= m;
                    }
                } else {
                    dp2[k & !(1 << j)] += dp[k] * if map[i][j] == -1 { 9 } else { 1 };
                    dp2[k & !(1 << j)] %= m;
                }
            }

            dp = dp2;
        }
    }

    let mut ans = 0u64;

    for i in 1 << (w - 1)..1 << w {
        ans += dp[i];
        ans %= m;
    }

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