結果

問題 No.1916 Making Palindrome on Gird
ユーザー logxlogx
提出日時 2022-04-29 21:49:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 656 ms / 3,000 ms
コード長 3,297 bytes
コンパイル時間 3,248 ms
コンパイル使用メモリ 217,964 KB
実行使用メモリ 170,452 KB
最終ジャッジ日時 2023-09-11 12:48:55
合計ジャッジ時間 9,453 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 57 ms
16,892 KB
testcase_16 AC 45 ms
13,872 KB
testcase_17 AC 109 ms
28,880 KB
testcase_18 AC 334 ms
74,556 KB
testcase_19 AC 322 ms
72,436 KB
testcase_20 AC 322 ms
72,244 KB
testcase_21 AC 331 ms
73,756 KB
testcase_22 AC 334 ms
74,032 KB
testcase_23 AC 3 ms
4,924 KB
testcase_24 AC 4 ms
5,004 KB
testcase_25 AC 3 ms
4,860 KB
testcase_26 AC 3 ms
4,856 KB
testcase_27 AC 3 ms
4,868 KB
testcase_28 AC 647 ms
170,412 KB
testcase_29 AC 650 ms
170,448 KB
testcase_30 AC 644 ms
170,400 KB
testcase_31 AC 651 ms
170,396 KB
testcase_32 AC 656 ms
170,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/modint>
using namespace atcoder;
using mint=modint1000000007;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define unless(x) if(!(x))
#define until(x) while(!(x))
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)

/*---------type/const---------*/
constexpr int big=1000000007;
//constexpr int big=998244353;
constexpr double EPS=1e-8; //適宜変える
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
constexpr int dx[4]={1,0,-1,0};
constexpr int dy[4]={0,1,0,-1};
constexpr char newl='\n';
struct{
    constexpr operator int(){return -int(1e9)-10;}
    constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
    constexpr operator int(){return int(1e9)+10;}
    constexpr operator ll(){return ll(1e18)+10;}
    constexpr auto operator -(){return neginf;}
}inf;

/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}



int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(10);
/*------------------------------------*/
    
    int h,w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i,h)cin >> s[i];
    if(h==1 && w==1){
        cout << 1 << newl;
        return 0;
    }
    vector dp(h,vector(w,map<pair<int,int>,mint>()));
    dp[0][0][{h-1,w-1}]=s[0][0]==s[h-1][w-1];
    mint ans=0;
    rep(i,h)rep(j,w)if(i+j<=(h+w-3)/2){
        for(auto [p,v]:dp[i][j]){
            if(i+j==(h+w-3)/2){
                rep(k,2)rep(l,2){
                    if(valid(i+dx[k],j+dy[k],h,w) && i+dx[k]+dx[l]==p.first && j+dy[k]+dy[l]==p.second)ans+=v;
                }
                rep(k,2)if(i+dx[k]==p.first && j+dy[k]==p.second)ans+=v;
            }
            else{
                auto [a,b]=p;
                if(i+1<h){
                    if(a>0 && s[i+1][j]==s[a-1][b])dp[i+1][j][{a-1,b}]+=v;
                    if(b>0 && s[i+1][j]==s[a][b-1])dp[i+1][j][{a,b-1}]+=v;
                }
                if(j+1<w){
                    if(a>0 && s[i][j+1]==s[a-1][b])dp[i][j+1][{a-1,b}]+=v;
                    if(b>0 && s[i][j+1]==s[a][b-1])dp[i][j+1][{a,b-1}]+=v;
                }
            }
        }
    }

    cout << ans.val() << newl;
}
0