結果

問題 No.1916 Making Palindrome on Gird
ユーザー phspls
提出日時 2022-09-19 20:10:39
言語 Rust
(1.83.0 + proconio)
結果
MLE  
実行時間 -
コード長 2,855 bytes
コンパイル時間 15,866 ms
コンパイル使用メモリ 379,688 KB
実行使用メモリ 816,512 KB
最終ジャッジ日時 2024-12-22 02:38:14
合計ジャッジ時間 40,420 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 MLE * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `VecDeque`
 --> src/main.rs:1:24
  |
1 | use std::collections::{VecDeque, HashSet};
  |                        ^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

ソースコード

diff #

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

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 = vec![vec![vec![vec![0usize; w]; h]; w]; h];
    let mut frontier = HashSet::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[x][y][x][y] = 1;
            frontier.insert((x, y, x, y));
        }
    } 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[x][y][nx][ny] += 1;
                        frontier.insert((x, y, nx, ny));
                    }
                }
            }
        }
    }
    for _ in 0..middle {
        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 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] {
                            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