結果

問題 No.1916 Making Palindrome on Gird
ユーザー inksamuraiinksamurai
提出日時 2022-04-30 20:03:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,956 bytes
コンパイル時間 2,437 ms
コンパイル使用メモリ 210,936 KB
実行使用メモリ 69,616 KB
最終ジャッジ日時 2023-09-12 12:27:42
合計ジャッジ時間 6,322 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 32 ms
37,428 KB
testcase_10 WA -
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 58 ms
35,784 KB
testcase_14 AC 32 ms
17,904 KB
testcase_15 AC 71 ms
37,228 KB
testcase_16 AC 66 ms
50,840 KB
testcase_17 AC 136 ms
65,096 KB
testcase_18 AC 157 ms
69,364 KB
testcase_19 AC 156 ms
69,376 KB
testcase_20 AC 157 ms
69,404 KB
testcase_21 AC 158 ms
69,372 KB
testcase_22 AC 159 ms
69,432 KB
testcase_23 AC 53 ms
68,260 KB
testcase_24 AC 53 ms
68,260 KB
testcase_25 AC 53 ms
68,312 KB
testcase_26 AC 55 ms
69,368 KB
testcase_27 AC 53 ms
68,772 KB
testcase_28 AC 179 ms
69,380 KB
testcase_29 AC 180 ms
69,372 KB
testcase_30 AC 182 ms
69,380 KB
testcase_31 AC 183 ms
69,616 KB
testcase_32 AC 182 ms
69,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3k5NXAD ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

//snuke's mod int
template <ll mod>
struct modint{
	ll x; // typedef long long ll;
	modint(ll x=0):x((x%mod+mod)%mod){}
	modint operator-()const{return modint(-x);}
	modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
	modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
	modint operator+(const modint a)const{modint res(*this); return res+=a;}
	modint operator-(const modint a)const{modint res(*this); return res-=a;}
	modint operator*(const modint a)const{modint res(*this); return res*=a;}
	modint pow(ll n)const{
		modint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modint inv()const{return pow(mod-2);}
};

using mint=modint<1000'000'007>;

signed main(){
_3k5NXAD;
	int h,w;
	std::cin>>h>>w;
	vec(string) a;
	rep(i,h){
		string s;
		std::cin>>s;
		a.emplace_back(s);
	}
	vec(vec(vec(mint))) dp(w+2,vec(vec(mint))(w+2,vec(mint)((h+w)/2+2)));
	if(a[0][0]!=a[h-1][w-1]){
		print(0);
		return 0;
	}
	dp[0][0][0]=1;
	rep(k,(h+w)/2+1){
		rep(i,w+1){
			if(i>k) break;
			rep(j,w+1){
				if(j>k) break;
				int sh=k-i,sw=i;
				int th=h-1-(k-j),tw=w-1-j;
				if(sh<0 or sh>=h or sw<0 or sw>=w) continue;
				if(th<0 or th>=h or tw<0 or tw>=w) continue;
				//rl
				if(sw+1<w and tw-1>=0 and a[sh][sw+1]==a[th][tw-1]){
					dp[i+1][j+1][k+1]+=dp[i][j][k];
				}
				//dl
				if(sh+1<h and tw-1>=0 and a[sh+1][sw]==a[th][tw-1]){
					dp[i][j+1][k+1]+=dp[i][j][k];
				}
				//ru
				if(sw+1<w and th-1>=0 and a[sh][sw+1]==a[th-1][tw]){
					dp[i+1][j][k+1]+=dp[i][j][k];
				}
				// du
				if(sh+1<h and th-1>=0 and a[sh+1][sw]==a[th-1][tw]){
					dp[i][j][k+1]+=dp[i][j][k];
				}
			}
		}
	}
	const int di[]={1,-1,0,0};
	const int dj[]={0,0,1,-1};
	auto adj=[&](int sx,int sy,int tx,int ty)->bool{
		rep(dir,4){
			int nx=sx+di[dir],ny=sy+dj[dir];
			if(nx==tx and ny==ty){
				return 1;
			}
		}
		return 0;
	};
	mint ans=0;
	int k=(h+w-1)/2-1;
	rep(i,w+1){
		rep(j,w+1){
			int sh=k-i,sw=i;
			int th=h-1-(k-j),tw=w-1-j;
			if(sh<0 or sh>=h or sw<0 or sw>=w) continue;
			if(th<0 or th>=h or tw<0 or tw>=w) continue;
			if((h+w-1)%2==0){
				if(adj(sh,sw,th,tw)){
					ans+=dp[i][j][k];
				}
			}else{
				rep(dir,4){
					int nx=sh+di[dir],ny=sw+dj[dir];
					if(adj(nx,ny,th,tw)){
						ans+=dp[i][j][k];
					}
				}
			}
		}
	}
	print(ans.x);
//
	return 0;
}
0