結果
問題 |
No.1916 Making Palindrome on Gird
|
ユーザー |
![]() |
提出日時 | 2022-04-30 20:04:44 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 194 ms / 3,000 ms |
コード長 | 3,003 bytes |
コンパイル時間 | 2,314 ms |
コンパイル使用メモリ | 207,428 KB |
最終ジャッジ日時 | 2025-01-29 00:55:40 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#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; }