結果

問題 No.1916 Making Palindrome on Gird
ユーザー 👑 rin204rin204
提出日時 2022-05-04 18:48:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,203 ms / 3,000 ms
コード長 1,136 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 156,524 KB
最終ジャッジ日時 2024-07-03 21:50:00
合計ジャッジ時間 11,042 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,536 KB
testcase_01 AC 39 ms
52,632 KB
testcase_02 AC 57 ms
66,736 KB
testcase_03 AC 36 ms
52,072 KB
testcase_04 AC 36 ms
53,200 KB
testcase_05 AC 38 ms
54,220 KB
testcase_06 AC 38 ms
54,380 KB
testcase_07 AC 37 ms
53,376 KB
testcase_08 AC 37 ms
53,244 KB
testcase_09 AC 37 ms
53,072 KB
testcase_10 AC 36 ms
53,232 KB
testcase_11 AC 37 ms
52,824 KB
testcase_12 AC 37 ms
52,988 KB
testcase_13 AC 61 ms
68,720 KB
testcase_14 AC 57 ms
68,640 KB
testcase_15 AC 135 ms
76,344 KB
testcase_16 AC 125 ms
76,352 KB
testcase_17 AC 204 ms
77,332 KB
testcase_18 AC 476 ms
95,444 KB
testcase_19 AC 485 ms
96,168 KB
testcase_20 AC 487 ms
95,724 KB
testcase_21 AC 478 ms
95,896 KB
testcase_22 AC 488 ms
95,700 KB
testcase_23 AC 36 ms
53,488 KB
testcase_24 AC 38 ms
53,632 KB
testcase_25 AC 38 ms
53,624 KB
testcase_26 AC 37 ms
54,260 KB
testcase_27 AC 38 ms
53,416 KB
testcase_28 AC 1,181 ms
155,644 KB
testcase_29 AC 1,166 ms
156,036 KB
testcase_30 AC 1,203 ms
156,048 KB
testcase_31 AC 1,195 ms
156,524 KB
testcase_32 AC 1,190 ms
155,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

h, w = map(int, input().split())
S = [input() for _ in range(h)]

if S[0][0] != S[-1][-1]:
    print(0)
    exit()

def f(i1, i2, j1, j2):
    return ((i1 * 200 + i2) * 200 + j1) * 200 + j2

def rf(x):
    j2 = x % 200
    x //= 200
    j1 = x % 200
    x //= 200
    i2 = x % 200
    i1 = x // 200
    return i1, i2, j1, j2

dp = {f(0, 0, h - 1, w - 1): 1}
directions = [(1, 0, -1, 0), (0, 1, -1, 0), (1, 0, 0, -1), (0, 1, 0, -1)]

for _ in range((h + w) // 2 - 1):
    ndp = {}
    for tmp, v in dp.items():
        i1, j1, i2, j2 = rf(tmp)
        for di1, dj1, di2, dj2 in directions:
            ni1 = i1 + di1
            nj1 = j1 + dj1
            ni2 = i2 + di2
            nj2 = j2 + dj2
            if ni1 == h or nj1 == w or ni2 == -1 or nj2 == -1:
                continue
            if S[ni1][nj1] == S[ni2][nj2]:
                tmp = f(ni1, nj1, ni2, nj2)
                ndp[tmp] = ndp.get(tmp, 0) + v
                ndp[tmp] %= MOD
    dp = ndp

ans = 0
for tmp, v in dp.items():
    i1, j1, i2, j2 = rf(tmp)
    if abs(i1 - i2) + abs(j1 - j2) <= 1:
        ans += v
        ans %= MOD
print(ans)

0