結果

問題 No.1916 Making Palindrome on Gird
ユーザー phsplsphspls
提出日時 2022-10-02 12:24:46
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 2,306 bytes
コンパイル時間 3,643 ms
コンパイル使用メモリ 146,116 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-26 07:12:44
合計ジャッジ時間 9,409 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 1 ms
4,380 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 243 ms
4,376 KB
testcase_19 AC 238 ms
4,380 KB
testcase_20 AC 244 ms
4,380 KB
testcase_21 AC 244 ms
4,376 KB
testcase_22 AC 238 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 612 ms
4,380 KB
testcase_29 AC 612 ms
4,380 KB
testcase_30 AC 614 ms
4,380 KB
testcase_31 AC 608 ms
4,376 KB
testcase_32 AC 613 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::HashMap;

const SDX: [isize; 2] = [-1, 0];
const SDY: [isize; 2] = [0, -1];
const GDX: [usize; 2] = [1, 0];
const GDY: [usize; 2] = [0, 1];
const MOD: usize = 1e9 as usize + 7;

fn main() {
    let mut hw = String::new();
    std::io::stdin().read_line(&mut hw).ok();
    let hw: Vec<usize> = hw.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let h = hw[0];
    let w = hw[1];
    let grid = (0..h).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            temp.trim().chars().collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();

    let mut mapping = HashMap::new();
    for i in 0..h.min(w) {
        let j = (h+w)/2-1-i;
        let (starti, startj) = if h > w { (i, j) } else { (j, i) };
        if (h + w) % 2 == 0 {
            mapping.insert((starti, startj, starti, startj), 1usize);
        } else {
            if starti+1 < h && grid[starti][startj] == grid[starti+1][startj] {
                *mapping.entry((starti, startj, starti+1, startj)).or_insert(0usize) += 1;
            }
            if startj+1 < w && grid[starti][startj] == grid[starti][startj+1] {
                *mapping.entry((starti, startj, starti, startj+1)).or_insert(0usize) += 1;
            }
        }
    }
    for _ in 0..(h+w)/2-1 {
        let mut next_mapping = HashMap::new();
        for (&(sx, sy, gx, gy), &cnt) in mapping.iter() {
            for sdir in 0..2 {
                let snx = sx as isize + SDX[sdir];
                let sny = sy as isize + SDY[sdir];
                if snx < 0 || sny < 0 { continue; }
                let snx = snx as usize;
                let sny = sny as usize;
                for gdir in 0..2 {
                    let gnx = gx + GDX[gdir];
                    let gny = gy + GDY[gdir];
                    if gnx == h || gny == w { continue; }
                    if grid[snx][sny] == grid[gnx][gny] {
                        *next_mapping.entry((snx, sny, gnx, gny)).or_insert(0usize) += cnt;
                        *next_mapping.entry((snx, sny, gnx, gny)).or_insert(0usize) %= MOD;
                    }
                }
            }
        }
        mapping = next_mapping;
    }
    println!("{}", mapping.get(&(0,0,h-1,w-1)).unwrap_or(&0usize));
}
0