結果

問題 No.1916 Making Palindrome on Gird
ユーザー phsplsphspls
提出日時 2022-09-19 20:14:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 624 ms / 3,000 ms
コード長 2,711 bytes
コンパイル時間 3,781 ms
コンパイル使用メモリ 160,268 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 19:03:39
合計ジャッジ時間 7,369 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 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 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 49 ms
4,376 KB
testcase_16 AC 26 ms
4,380 KB
testcase_17 AC 92 ms
4,376 KB
testcase_18 AC 249 ms
4,380 KB
testcase_19 AC 238 ms
4,376 KB
testcase_20 AC 245 ms
4,376 KB
testcase_21 AC 239 ms
4,380 KB
testcase_22 AC 240 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 624 ms
4,376 KB
testcase_29 AC 609 ms
4,376 KB
testcase_30 AC 610 ms
4,376 KB
testcase_31 AC 617 ms
4,380 KB
testcase_32 AC 613 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused imports: `HashSet`, `VecDeque`
 --> Main.rs:1:24
  |
1 | use std::collections::{VecDeque, HashSet, HashMap};
  |                        ^^^^^^^^  ^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::collections::{VecDeque, HashSet, HashMap};

const SDX: [isize; 2] = [1, 0];
const SDY: [isize; 2] = [0, 1];
const EDX: [isize; 2] = [-1, 0];
const EDY: [isize; 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 s = String::new();
            std::io::stdin().read_line(&mut s).ok();
            let s = s.trim();
            s.chars().collect::<Vec<char>>()
        })
        .collect::<Vec<_>>();

    let mut dp = HashMap::new();
    let middle = (h+w)/2-1;
    if (h+w) % 2 == 0 {
        for i in 0..h.min(w) {
            let (x, y) = if h > w { (middle-i, i) } else { (i, middle-i) };
            dp.insert((x, y, x, y), 1);
        }
    } else {
        for i in 0..h.min(w) {
            let (x, y) = if h > w { (middle - i, i) } else { (i, middle - i) };
            for dir in 0..2 {
                let nx = x as isize + SDX[dir];
                let ny = y as isize + SDY[dir];
                if nx >= 0 && ny >= 0 && nx < h as isize && ny < w as isize {
                    let nx = nx as usize;
                    let ny = ny as usize;
                    if grid[x][y] == grid[nx][ny] {
                        *dp.entry((x, y, nx, ny)).or_insert(0usize) += 1;
                    }
                }
            }
        }
    }
    for _ in 0..middle {
        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 + EDX[sdir];
                let nsy = sy as isize + EDY[sdir];
                if !(nsx >= 0 && nsy >= 0 && nsx < h as isize && nsy < w as isize) { continue; }
                let nsx = nsx as usize;
                let nsy = nsy as usize;
                for edir in 0..2 {
                    let ngx = gx as isize + SDX[edir];
                    let ngy = gy as isize + SDY[edir];
                    if ngx >= 0 && ngy >= 0 && ngx < h as isize && ngy < w as isize {
                        let ngx = ngx as usize;
                        let ngy = ngy as usize;
                        if grid[nsx][nsy] == grid[ngx][ngy] {
                            *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;
    }
    println!("{}", dp.get(&(0,0,h-1,w-1)).unwrap_or(&0));
}
0