結果
| 問題 |
No.1916 Making Palindrome on Gird
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-02 12:12:22 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,418 bytes |
| コンパイル時間 | 17,054 ms |
| コンパイル使用メモリ | 378,128 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-12-24 22:21:52 |
| 合計ジャッジ時間 | 59,898 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 RE * 6 TLE * 10 |
ソースコード
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 result = 0usize;
let mut mapping = HashMap::new();
for starti in 0..h.min(w) {
let startj = (h+w)/2-1-starti;
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.insert((starti, startj, starti+1, startj), 1usize);
}
if startj+1 < w && grid[starti][startj] == grid[starti][startj+1] {
mapping.insert((starti, startj, starti, startj+1), 1usize);
}
}
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;
}
if let Some(&x) = mapping.get(&(0,0,h-1,w-1)) {
result += x;
result %= MOD;
}
}
println!("{}", result);
}