結果

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

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long mod=998244353;
const long long mod2=469762049;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    int N;
    cin>>N;
    char c[N+1][N+1];
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++) cin>>c[i][j];
    }
    if(c[1][1]!=c[N][N]){
        cout<<"0\n";
        return 0;
    }
    ll dp[N+1][N+1][N+1];//i回移動したときに左上スタートが(j,?),右下スタートが(k,?)の数
    for(int i=0;i<=N;i++){
        for(int j=0;j<=N;j++){
            for(int k=0;k<=N;k++) dp[i][j][k]=0;
        }
    }
    dp[0][1][N]=1;
    const bool dx[4]={1,1,0,0},dy[4]={0,1,0,1};
    for(int i=0;i<N;i++){
        for(int j=1;j<=N;j++){
            for(int k=1;k<=N;k++){
                if(dp[i][j][k]==0) continue;
                int r1=j,c1=i+2-j;
                int r2=k,c2=2*N-i-k;
                if(c1<=0 || c1>N || c2<=0 || c2>N) continue;
                for(int l=0;l<4;l++){
                    int nr1=r1,nc1=c1,nr2=r2,nc2=c2;
                    if(dx[l]) nr1++;
                    else nc1++;
                    if(dy[l]) nr2--;
                    else nc2--;
                    if(nr1>N || nc1>N || nr2<=0 || nc2<=0) continue;
                    if(c[nr1][nc1]!=c[nr2][nc2]) continue;
                    dp[i+1][nr1][nr2]=(dp[i+1][nr1][nr2]+dp[i][r1][r2])%mod;
                }
            }
        }
    }
    ll ans=0;
    for(int i=1;i<=N;i++){
        ans=(ans+dp[N-1][i][i])%mod;
    }
    cout<<ans<<endl;
}
0