結果

問題 No.1916 Making Palindrome on Gird
ユーザー phsplsphspls
提出日時 2022-10-16 12:20:16
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 2,304 bytes
コンパイル時間 1,813 ms
コンパイル使用メモリ 160,904 KB
実行使用メモリ 7,732 KB
最終ジャッジ日時 2023-09-09 15:51:13
合計ジャッジ時間 11,162 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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();
            let temp = temp.trim();
            temp.chars().collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();

    let times = (h+w)/2-1;
    let mut result = 0usize;
    for i in 0..h.min(w) {
        let mut dp = HashMap::new();
        let x = if h < w { i } else { times-i };
        let y = if h < w { times-i } else { i };
        if (h + w) % 2 == 0 {
            dp.insert((x, y, x, y), 1usize);
        } else {
            if x+1 < h && grid[x][y] == grid[x+1][y] {
                dp.insert((x, y, x+1, y), 1usize);
            }
            if y+1 < w && grid[x][y] == grid[x][y+1] {
                dp.insert((x, y, x, y+1), 1usize);
            }
        };
        for _ in 0..times {
            let mut next_dp = HashMap::new();
            for (&(sx, sy, gx, gy), &cnt) in dp.iter() {
                for sdir in 0..2 {
                    let nsx = sx as isize + SDX[sdir];
                    let nsy = sy as isize + SDY[sdir];
                    if !(nsx >= 0 && nsy >= 0) { continue; }
                    let nsx = nsx as usize;
                    let nsy = nsy as usize;
                    for gdir in 0..2 {
                        let ngx = gx + GDX[gdir];
                        let ngy = gy + GDY[gdir];
                        if !(ngx < h && ngy < w) { continue; }
                        if grid[nsx][nsy] != grid[ngx][ngy] { continue; }
                        *next_dp.entry((nsx, nsy, ngx, ngy)).or_insert(0usize) += cnt;
                        *next_dp.entry((nsx, nsy, ngx, ngy)).or_insert(0usize) %= MOD;
                    }
                }
            }
            dp = next_dp;
        }
        result += dp.values().sum::<usize>();
        result %= MOD;
    }
    println!("{}", result);
}
0