結果

問題 No.1916 Making Palindrome on Gird
ユーザー 回転
提出日時 2026-11-12 13:11:20
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,177 ms / 3,000 ms
+ 626µs
コード長 1,080 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 601 ms
コンパイル使用メモリ 64,132 KB
実行使用メモリ 183,816 KB
最終ジャッジ日時 2026-11-12 13:11:35
合計ジャッジ時間 12,594 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import pypyjit
pypyjit.set_param("max_unroll_recursion=-1")
import sys
sys.setrecursionlimit(10**7)
MOD = 10**9+7
H,W = list(map(int,input().split()))
S = [input() for _ in range(H)]

dy = [0,1]
dx = [1,0]
ddy = [0,-1]
ddx = [-1,0]
memo = [[[-1 for _ in range(H)] for _ in range(W)] for _ in range(H)]
def f(y,x,yy):
    xx = (H-1 + W-1) - yy - x - y
    if(y+x == (H+W-2)//2):return int(abs(y - yy) + abs(x - xx) <= 1)
    if(memo[y][x][yy] != -1):return memo[y][x][yy]
    ret = 0
    for i in range(2):
        next_y = y + dy[i]
        next_x = x + dx[i]
        if(next_y < 0 or H <= next_y):continue
        if(next_x < 0 or W <= next_x):continue
        for j in range(2):
            next_yy = yy + ddy[j]
            next_xx = xx + ddx[j]
            if(next_yy < 0 or H <= next_yy):continue
            if(next_xx < 0 or W <= next_xx):continue
            if(S[next_y][next_x] != S[next_yy][next_xx]):continue
            ret += f(next_y, next_x, next_yy)
            ret %= MOD

    memo[y][x][yy] = ret
    return ret

print(f(0,0,H-1) if S[0][0] == S[-1][-1] else 0)
0