結果

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

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using mint = atcoder::modint998244353;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<string> A(n);
    for(auto &&str : A) cin >> str;
    if(A[0][0] != A[n - 1][n - 1]){
        cout << "0\n";
        return 0;
    }
	auto B = A;
	reverse(B.begin(), B.end());
	for(auto &&str : B) reverse(str.begin(), str.end());
    vector dp(1, vector<mint>(1));
	dp[0][0] = 1;
    for(int a = 0; a + 1 < n; a++){
		vector ndp(a + 2, vector<mint>(a + 2));
		for(int y1 = 0; y1 <= a; y1++){
			int x1 = a - y1;
			for(int y2 = 0; y2 <= a; y2++){
				int x2 = a - y2;
				if(A[y1][x1] != B[y2][x2]) continue;
				if(A[y1 + 1][x1] == B[y2][x2 + 1]) ndp[y1 + 1][y2] += dp[y1][y2];
				if(A[y1 + 1][x1] == B[y2 + 1][x2]) ndp[y1 + 1][y2 + 1] += dp[y1][y2];
				if(A[y1][x1 + 1] == B[y2 + 1][x2]) ndp[y1][y2 + 1] += dp[y1][y2];
				if(A[y1][x1 + 1] == B[y2][x2 + 1]) ndp[y1][y2] += dp[y1][y2];
			}
		}
		swap(dp, ndp);
	}
	mint ans;
	for(int y1 = 0; y1 < n; y1++){
		int x1 = n - 1 - y1;
		int y2 = n - 1 - y1;
		int x2 = n - 1 - x1;
		ans += dp[y1][y2];
	}
	cout << ans.val() << '\n';
}
0