結果

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

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
using mint = modint998244353;
const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
const int dx0[4] = { 1,0 };
const int dy0[4] = { 0,1 };
const int dx1[4] = { -1,0 };
const int dy1[4] = { 0,-1 };
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n; cin >> n;
	vector<string>s(n);
	rep(i, n) cin >> s[i];
	if (s[0][0] != s[n - 1][n - 1]) {
		cout << 0 << endl;
		return 0;
	}
	map<pair<P, P>, mint>dp;
	dp[{ {0, 0}, { n - 1,n - 1 }}] = 1;
	rep(i, n - 1) {
		map<pair<P, P>, mint>ndp;
		for (auto[k, v] : dp) {
			auto p0 = k.first;
			auto p1 = k.second;
			//cout << p0.first << " " << p0.second << ":" << p1.first << " " << p1.second << endl;
			//cout << v.val() << endl;
			rep(i, 2)rep(j, 2) {
				auto cp0 = p0;
				auto cp1 = p1;
				cp0.first += dx0[i];
				cp0.second += dy0[i];
				if (cp0.first < 0 || cp0.first >= n)continue;
				if (cp0.second < 0 || cp0.second >= n)continue;

				cp1.first += dx1[j];
				cp1.second += dy1[j];
				if (cp1.first < 0 || cp1.first >= n)continue;
				if (cp1.second < 0 || cp1.second >= n)continue;
				if (s[cp0.first][cp0.second] != s[cp1.first][cp1.second])continue;
				ndp[{cp0, cp1}] += v;
			}
		}
		swap(dp, ndp);
	}
	mint ans = 0;
	for (auto[k, v] : dp)if (k.first == k.second)ans += v;
	cout << ans.val() << endl;
	return 0;
}
0