結果

問題 No.1916 Making Palindrome on Gird
ユーザー phsplsphspls
提出日時 2022-09-18 23:18:34
言語 Rust
(1.77.0)
結果
MLE  
実行時間 -
コード長 2,996 bytes
コンパイル時間 3,288 ms
コンパイル使用メモリ 159,580 KB
実行使用メモリ 812,320 KB
最終ジャッジ日時 2023-08-23 18:24:42
合計ジャッジ時間 6,808 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `HashMap`
 --> Main.rs:1:24
  |
1 | use std::collections::{HashMap, HashSet};
  |                        ^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: 1 warning emitted

ソースコード

diff #

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


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

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().map(|c| c as usize - 'a' as usize).collect::<Vec<usize>>()
        })
        .collect::<Vec<Vec<usize>>>();

    if grid[0][0] != grid[h-1][w-1] {
        println!("0");
        return;
    }
    let mut dp = vec![vec![vec![vec![0usize; w]; h]; w]; h];
    let mut frontier = HashSet::new();
    if (h+w) % 2 == 0 {
        for i in 0..h.min(w) {
            let x = if h > w { (h+w)/2-1-i } else { i };
            let y = if w >= h { (h+w)/2-1-i } else { i };
            dp[x][y][x][y] = 1;
            frontier.insert((x, y, x, y));
        }
    } else {
        for i in 0..h.min(w) {
            let x = if h > w { (h+w)/2-1-i } else { i };
            let y = if w >= h { (h+w)/2-1-i } else { 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[x][y][nx][ny] = 1;
                        frontier.insert((x, y, nx, ny));
                    }
                }
            }
        }
    }
    for _ in 0..(h+w)/2-1 {
        let mut next_frontier = HashSet::new();
        for &(sx, sy, gx, gy) in frontier.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 gdir in 0..2 {
                    let ngx = gx as isize + SDX[gdir];
                    let ngy = gy as isize + SDY[gdir];
                    if !(ngx >= 0 && ngy >= 0 && ngx < h as isize && ngy < w as isize) { continue; }
                    let ngx = ngx as usize;
                    let ngy = ngy as usize;
                    if grid[nsx][nsy] == grid[ngx][ngy] {
                        dp[nsx][nsy][ngx][ngy] += dp[sx][sy][gx][gy];
                        dp[nsx][nsy][ngx][ngy] %= MOD;
                        next_frontier.insert((nsx, nsy, ngx, ngy));
                    }
                }
            }
        }
        frontier = next_frontier;
    }
    println!("{}", dp[0][0][h-1][w-1]);
}
0