結果

問題 No.1916 Making Palindrome on Gird
ユーザー rlangevinrlangevin
提出日時 2023-10-10 00:15:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,569 ms / 3,000 ms
コード長 1,648 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 283,244 KB
最終ジャッジ日時 2023-10-10 00:15:42
合計ジャッジ時間 23,412 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,504 KB
testcase_01 AC 99 ms
71,376 KB
testcase_02 AC 134 ms
77,620 KB
testcase_03 AC 99 ms
71,344 KB
testcase_04 AC 99 ms
71,624 KB
testcase_05 AC 100 ms
71,416 KB
testcase_06 AC 103 ms
71,664 KB
testcase_07 AC 104 ms
71,152 KB
testcase_08 AC 101 ms
71,668 KB
testcase_09 AC 98 ms
71,340 KB
testcase_10 AC 96 ms
71,344 KB
testcase_11 AC 103 ms
72,184 KB
testcase_12 AC 99 ms
71,152 KB
testcase_13 AC 132 ms
78,680 KB
testcase_14 AC 125 ms
78,224 KB
testcase_15 AC 339 ms
101,176 KB
testcase_16 AC 246 ms
96,248 KB
testcase_17 AC 477 ms
116,040 KB
testcase_18 AC 1,184 ms
213,580 KB
testcase_19 AC 1,070 ms
190,864 KB
testcase_20 AC 1,143 ms
193,420 KB
testcase_21 AC 1,176 ms
212,284 KB
testcase_22 AC 1,122 ms
191,580 KB
testcase_23 AC 107 ms
73,896 KB
testcase_24 AC 106 ms
73,956 KB
testcase_25 AC 102 ms
73,924 KB
testcase_26 AC 102 ms
73,992 KB
testcase_27 AC 106 ms
73,812 KB
testcase_28 AC 2,547 ms
282,504 KB
testcase_29 AC 2,569 ms
282,628 KB
testcase_30 AC 2,515 ms
282,748 KB
testcase_31 AC 2,530 ms
282,892 KB
testcase_32 AC 2,525 ms
283,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

M = 205
def encode(a, b, c, d):
    now = a
    now *= M
    now += b
    now *= M
    now += c
    now *= M
    now += d
    return now

def decode(v):
    d = v % M
    v //= M
    c = v % M
    v //= M
    b = v % M
    v //= M
    return (v, b, c, d)

H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))
    
from collections import *
Q = deque()
if S[0][0] != S[-1][-1]:
    print(0)
    exit()
    
mod = 10**9 + 7
Q.append(encode(0, 0, H - 1, W - 1))
D = defaultdict(int)
D[encode(0, 0, H - 1, W - 1)] = 1
dx = [1, 0]
dy = [0, 1]
ans = 0
SS = set()
seen = set()
while Q:
    t = Q.popleft()
    sx, sy, gx, gy = decode(t)
    if gx + gy <= sx + sy + 1:
        if abs(sx - gx) + abs(sy - gy) <= 1 and t not in SS:
            SS.add(t)
            ans += D[encode(sx, sy, gx, gy)]
            ans %= mod
        continue
    
    for a in range(2):
        for b in range(2):
            nsx = sx + dx[a]
            nsy = sy + dy[a]
            ngx = gx - dx[b]
            ngy = gy - dy[b]
            d = abs(sx - gx) + abs(sy - gy)
            nd = abs(nsx - ngx) + abs(nsy - ngy)
            if nsx < 0 or nsx > H - 1 or nsy < 0 or nsy > W - 1:
                continue
            if ngx < 0 or ngx > H - 1 or ngy < 0 or ngy > W - 1:
                continue
            if nd != d - 2:
                continue
            if S[nsx][nsy] != S[ngx][ngy]:
                continue
            nt = encode(nsx, nsy, ngx, ngy)
            D[nt] += D[t]
            D[nt] %= mod
            if nt in seen:
                continue
            seen.add(nt)
            Q.append(nt)
            
print(ans)
0