結果

問題 No.1916 Making Palindrome on Gird
ユーザー ruthen71
提出日時 2022-04-29 22:41:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 682 ms / 3,000 ms
コード長 1,828 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 206,316 KB
最終ジャッジ日時 2025-01-28 23:29:05
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

#include <atcoder/modint>
using mint = atcoder::modint1000000007;
ostream &operator<<(ostream &os, const mint &p) { return os << p.val(); }

map<int, mint> dp[200][200];

int f(int x, int y) { return x * 200 + y; }
pair<int, int> finv(int k) { return {k / 200, k % 200}; }

const int dx[2] = {0, 1}, dy[2] = {1, 0};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int H, W;
    cin >> H >> W;
    V<string> S(H);
    rep(i, H) cin >> S[i];
    dp[0][0][f(H - 1, W - 1)] = 1;
    mint ans = 0;
    for (int k = 0; k < (H + W) / 2; k++) {
        for (int i = 0; i < H; i++) {
            int j = k - i;
            if (!(0 <= j && j < W)) continue;
            // show(i, j, dp[i][j]);
            // dp[i][j] -> dp[i+1][j] or dp[i][j+1]
            for (auto [k, val] : dp[i][j]) {
                auto [cx, cy] = finv(k);
                if (abs(i - cx) + abs(j - cy) <= 1) {
                    ans += val;
                    // show(ans);
                    continue;
                }
                rep(a, 2) {
                    rep(b, 2) {
                        int nx = i + dx[a], ny = j + dy[a];
                        int ncx = cx - dx[b], ncy = cy - dy[b];
                        if (!(0 <= nx && nx < H && 0 <= ny && ny < W)) continue;
                        if (!(0 <= ncx && ncx < H && 0 <= ncy && ncy < W)) continue;
                        if (S[nx][ny] != S[ncx][ncy]) continue;
                        dp[nx][ny][f(ncx, ncy)] += val;
                    }
                }
            }
        }
    }
    cout << ans.val() << '\n';
    return 0;
}
0