結果

問題 No.1916 Making Palindrome on Gird
ユーザー hiromi_ayasehiromi_ayase
提出日時 2022-04-30 02:59:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 77 ms / 3,000 ms
コード長 2,063 bytes
コンパイル時間 1,519 ms
コンパイル使用メモリ 146,532 KB
実行使用メモリ 35,036 KB
最終ジャッジ日時 2023-09-11 19:16:44
合計ジャッジ時間 3,562 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 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,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 27 ms
13,912 KB
testcase_14 AC 18 ms
9,872 KB
testcase_15 AC 36 ms
16,948 KB
testcase_16 AC 23 ms
10,664 KB
testcase_17 AC 65 ms
30,604 KB
testcase_18 AC 74 ms
34,984 KB
testcase_19 AC 73 ms
35,036 KB
testcase_20 AC 73 ms
34,956 KB
testcase_21 AC 72 ms
35,036 KB
testcase_22 AC 74 ms
35,012 KB
testcase_23 AC 2 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,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 77 ms
35,028 KB
testcase_29 AC 77 ms
35,000 KB
testcase_30 AC 77 ms
35,028 KB
testcase_31 AC 77 ms
35,020 KB
testcase_32 AC 77 ms
34,980 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 - 2) / 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 {
            for k in 0..h {
                if i + j == v {
                    ans += dp[i][j][k];
                    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