結果

問題 No.1916 Making Palindrome on Gird
ユーザー sahiyasahiya
提出日時 2022-04-29 23:44:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,230 bytes
コンパイル時間 2,018 ms
コンパイル使用メモリ 137,276 KB
実行使用メモリ 67,996 KB
最終ジャッジ日時 2023-09-11 15:25:31
合計ジャッジ時間 5,360 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 63 ms
37,820 KB
testcase_14 AC 43 ms
27,632 KB
testcase_15 AC 75 ms
42,508 KB
testcase_16 AC 55 ms
35,952 KB
testcase_17 AC 120 ms
62,852 KB
testcase_18 AC 125 ms
67,948 KB
testcase_19 AC 127 ms
67,996 KB
testcase_20 AC 127 ms
67,940 KB
testcase_21 AC 125 ms
67,944 KB
testcase_22 AC 124 ms
67,980 KB
testcase_23 AC 51 ms
21,152 KB
testcase_24 AC 46 ms
17,904 KB
testcase_25 AC 42 ms
15,064 KB
testcase_26 AC 48 ms
19,656 KB
testcase_27 AC 49 ms
19,324 KB
testcase_28 AC 125 ms
67,956 KB
testcase_29 AC 124 ms
67,944 KB
testcase_30 AC 125 ms
67,944 KB
testcase_31 AC 125 ms
67,956 KB
testcase_32 AC 126 ms
67,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define ALL(x) (x).begin(), (x).end()
#define PC(x) __builtin_popcount(x)
#define PCL(x) __builtin_popcountll(x)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
struct edge {
    int to, cost, id;
};
const double PI = 3.14159265358979323846;
const double PI2 = PI * 2.0;
const double EPS = 1E-09;
const ll MOD = 1E+09 + 7; // =998244353;
const ll INFL = 1E18;
const int INFI = 1E09;
const int MAX_N = 2E+02;

ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

int H, W;

char A[MAX_N + 2][MAX_N + 2];

ll dp[MAX_N + 3][MAX_N + 3][MAX_N + 3];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> H >> W;

    for (int i = 1; i <= H; ++i) {
        string s;
        cin >> s;
        for (int j = 1; j <= W; ++j) {
            A[i][j] = s[j - 1];
        }
    }

    if (A[1][1] == A[H][W])
        dp[1][1][H] = 1;
    for (int i = 1; i <= H; ++i) {
        for (int j = 1; j <= W; ++j) {
            // if (i + j >= (H + W - 1) / 2)
            //     break;
            for (int k = H; k >= 1; k--) {
                int l = H + W - k - (i + j - 2);
                if (l < 1 || l > W)
                    continue;
                char x = A[i + 1][j];
                char y = A[i][j + 1];
                char z = A[k][l - 1];
                char w = A[k - 1][l];
                if (x == z) {
                    dp[i + 1][j][k] += dp[i][j][k];
                    dp[i + 1][j][k] %= MOD;
                }
                if (x == w) {
                    dp[i + 1][j][k - 1] += dp[i][j][k];
                    dp[i + 1][j][k - 1] %= MOD;
                }
                if (y == z) {
                    dp[i][j + 1][k] += dp[i][j][k];
                    dp[i][j + 1][k] %= MOD;
                }
                if (y == w) {
                    dp[i][j + 1][k - 1] += dp[i][j][k];
                    dp[i][j + 1][k - 1] %= MOD;
                }
            }
        }
    }

    ll ans = 0;
    int N = H + W - 1;
    if (N % 2 == 0) {
        for (int i = 1; i <= H; ++i) {
            int j = N / 2 - i + 1;
            if (j < 1 || j > W)
                break;
            ans += dp[i][j][i + 1] + dp[i][j][i];
            ans %= MOD;
        }
    } else {
        for (int i = 1; i <= H; ++i) {
            int j = N / 2 - i + 1;
            if (j < 1 || j > W)
                break;
            ans += dp[i][j][i + 1] * 2 + dp[i][j][i] + dp[i][j][i + 2];
            ans %= MOD;
        }
    }

    // for (int i = 0; i < N; i++) {
    //     for (int j = 0; j < N; j++) {
    //         cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
    //     }
    // }

    cout << ans << "\n";

    return 0;
}
0