結果

問題 No.1916 Making Palindrome on Gird
ユーザー hiromi_ayasehiromi_ayase
提出日時 2022-04-30 02:53:20
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,005 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 148,232 KB
実行使用メモリ 35,036 KB
最終ジャッジ日時 2023-09-11 19:09:34
合計ジャッジ時間 3,262 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 18 ms
9,864 KB
testcase_15 WA -
testcase_16 AC 23 ms
10,664 KB
testcase_17 AC 63 ms
30,676 KB
testcase_18 AC 71 ms
35,032 KB
testcase_19 AC 73 ms
35,016 KB
testcase_20 AC 71 ms
34,956 KB
testcase_21 AC 73 ms
34,980 KB
testcase_22 AC 73 ms
35,024 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 76 ms
35,036 KB
testcase_29 AC 77 ms
34,944 KB
testcase_30 AC 77 ms
34,976 KB
testcase_31 AC 75 ms
35,016 KB
testcase_32 AC 75 ms
35,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(clippy::many_single_char_names)]
fn main() {
    let s = getline();
    let a = s.split(' ').collect::<Vec<_>>();
    let h = a[0].parse::<usize>().unwrap();
    let w = a[1].parse::<usize>().unwrap();
    let mut s = vec![vec!['a'; w]; h];

    for i in 0..h {
        s[i] = getline().chars().collect();
    }

    if s[0][0] != s[h - 1][w - 1] {
        println!("0");
        return;
    }

    let mut dp = vec![vec![vec![0; h]; w]; h];
    dp[0][0][h - 1] = 1;

    let v = (h + w - 1) / 2;

    let m = 1_000_000_007;

    for i in 0..h {
        for j in 0..w {
            if i + j >= v {
                continue;
            }

            for ni in (0..h).rev() {
                // i + j = (h-1-ni) + (w-1-nj)
                let nj = w + h - (i + j) - 2 - ni;
                if nj >= w || i > ni || j > nj {
                    continue;
                }
                if i + 1 < ni && s[i + 1][j] == s[ni - 1][nj] {
                    dp[i + 1][j][ni - 1] += dp[i][j][ni];
                    dp[i + 1][j][ni - 1] %= m;
                }
                if i < ni && j < nj && s[i + 1][j] == s[ni][nj - 1] {
                    dp[i + 1][j][ni] += dp[i][j][ni];
                    dp[i + 1][j][ni] %= m;
                }
                if i < ni && j < nj && s[i][j + 1] == s[ni - 1][nj] {
                    dp[i][j + 1][ni - 1] += dp[i][j][ni];
                    dp[i][j + 1][ni - 1] %= m;
                }
                if j + 1 < nj && s[i][j + 1] == s[ni][nj - 1] {
                    dp[i][j + 1][ni] += dp[i][j][ni];
                    dp[i][j + 1][ni] %= m;
                }
            }
        }
    }

    let mut ans = 0;
    for i in 0..h {
        for j in 0..w {
            if i + j == v {
                ans += dp[i][j][i];
                ans %= m;
            }
        }
    }
    println!("{}", ans);
}

fn getline() -> String {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim().to_string()
}
0