結果

問題 No.3429 Palindromic Path (Hard)
コンテスト
ユーザー shingo0909
提出日時 2026-01-11 14:33:04
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 988 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,541 ms
コンパイル使用メモリ 351,640 KB
実行使用メモリ 67,712 KB
最終ジャッジ日時 2026-01-11 14:33:09
合計ジャッジ時間 4,129 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using mint = atcoder::modint998244353;

struct S{
    mint a;
    bool x=false;
};

int main(){
    int n;
    cin >> n;
    vector<string>s(n);
    rep(i,n)cin >> s[i];
    vector memo(n,vector(n,vector<S>(n)));
    auto dfs=[&](tuple<int,int,int,int> in, auto &dfs){
        auto [x,y,a,b]=in;
        if(x+y == a+b){
            return mint(x==a?1:0);
        }
        if(x>a || y>b)return mint(0);
        if(s[x][y]!=s[a][b])return mint(0);
        if(memo[x][y][a].x)return memo[x][y][a].a;
        mint ans=0;
        ans+=dfs(make_tuple(x+1,y,a-1,b),dfs);
        ans+=dfs(make_tuple(x,y+1,a-1,b),dfs);
        ans+=dfs(make_tuple(x+1,y,a,b-1),dfs);
        ans+=dfs(make_tuple(x,y+1,a,b-1),dfs);
        memo[x][y][a].x=true;
        return memo[x][y][a].a=ans;
    };
    cout <<  dfs(make_tuple(0,0,n-1,n-1),dfs).val() << endl;
}
0