結果

問題 No.1916 Making Palindrome on Gird
ユーザー ぷらぷら
提出日時 2022-04-29 21:39:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 3,000 ms
コード長 1,855 bytes
コンパイル時間 2,139 ms
コンパイル使用メモリ 202,124 KB
実行使用メモリ 20,068 KB
最終ジャッジ日時 2023-09-11 12:34:27
合計ジャッジ時間 4,687 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 38 ms
12,348 KB
testcase_14 AC 25 ms
9,952 KB
testcase_15 AC 45 ms
13,404 KB
testcase_16 AC 32 ms
11,880 KB
testcase_17 AC 80 ms
18,604 KB
testcase_18 AC 90 ms
19,892 KB
testcase_19 AC 91 ms
19,940 KB
testcase_20 AC 90 ms
20,012 KB
testcase_21 AC 91 ms
19,940 KB
testcase_22 AC 91 ms
19,924 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 93 ms
19,940 KB
testcase_29 AC 93 ms
19,896 KB
testcase_30 AC 93 ms
19,908 KB
testcase_31 AC 92 ms
19,880 KB
testcase_32 AC 93 ms
20,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

constexpr int mod = 1000000007;

int dx1[4] = {0,0,1,1};
int dy1[4] = {1,1,0,0};
int dx2[4] = {0,-1,0,-1};
int dy2[4] = {-1,0,-1,0};

int dp[201][201][201];

int main() {
    int H,W;
    cin >> H >> W;
    vector<string>S(H);
    for(int i = 0; i < H; i++) {
        cin >> S[i];
    }
    if(S[0][0] != S[H-1][W-1]) {
        cout << 0 << endl;
        return 0;
    }
    dp[0][0][H-1] = 1;
    for(int i = 0; i < (H+W)/2-1; i++) {
        for(int j = 0; j < H; j++) {
            for(int k = 0; k < H; k++) {
                int j2 = i-j;
                int k2 = H+W-2-k-i;
                if(j2 >= 0 && k2 >= 0 && j2 < W && k2 < W) {
                    for(int l = 0; l < 4; l++) {
                        int nx1 = j+dx1[l];
                        int ny1 = j2+dy1[l];
                        int nx2 = k+dx2[l];
                        int ny2 = k2+dy2[l];
                        if(min({nx1,ny1,nx2,ny2}) >= 0 && max(nx1,nx2) < H && max(ny1,ny2) < W && S[nx1][ny1] == S[nx2][ny2]) {
                            dp[i+1][nx1][nx2] += dp[i][j][k];
                            if(dp[i+1][nx1][nx2] >= mod) {
                                dp[i+1][nx1][nx2] -= mod;
                            }
                        }
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < H; j++) {
            if((H+W)%2 == 0 && i == j) {
                ans += dp[(H+W)/2-1][i][j];
                if(ans >= mod) {
                    ans -= mod;
                }
            }
            if((H+W)%2 == 1 && (i+1 == j || i == j)) {
                ans += dp[(H+W)/2-1][i][j];
                if(ans >= mod) {
                    ans -= mod;
                }
            }
        }
    }
    cout << ans << endl;
}
0