結果

問題 No.1916 Making Palindrome on Gird
ユーザー inksamuraiinksamurai
提出日時 2022-04-30 20:04:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 3,000 ms
コード長 3,003 bytes
コンパイル時間 4,528 ms
コンパイル使用メモリ 211,152 KB
実行使用メモリ 69,780 KB
最終ジャッジ日時 2023-09-12 12:28:58
合計ジャッジ時間 7,802 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 32 ms
37,472 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 60 ms
35,600 KB
testcase_14 AC 32 ms
17,816 KB
testcase_15 AC 74 ms
37,196 KB
testcase_16 AC 67 ms
50,844 KB
testcase_17 AC 142 ms
65,100 KB
testcase_18 AC 173 ms
69,420 KB
testcase_19 AC 165 ms
69,408 KB
testcase_20 AC 168 ms
69,412 KB
testcase_21 AC 169 ms
69,344 KB
testcase_22 AC 168 ms
69,780 KB
testcase_23 AC 52 ms
68,236 KB
testcase_24 AC 52 ms
68,300 KB
testcase_25 AC 52 ms
68,240 KB
testcase_26 AC 53 ms
69,684 KB
testcase_27 AC 54 ms
68,760 KB
testcase_28 AC 200 ms
69,396 KB
testcase_29 AC 201 ms
69,348 KB
testcase_30 AC 198 ms
69,428 KB
testcase_31 AC 202 ms
69,352 KB
testcase_32 AC 201 ms
69,680 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;
	}
	if(h==1 and w==1){
		print(1);
		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