結果

問題 No.1916 Making Palindrome on Gird
ユーザー 👑 rin204rin204
提出日時 2022-05-04 18:48:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,162 ms / 3,000 ms
コード長 1,136 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 86,836 KB
実行使用メモリ 157,072 KB
最終ジャッジ日時 2023-09-16 23:01:35
合計ジャッジ時間 11,789 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,296 KB
testcase_01 AC 65 ms
71,340 KB
testcase_02 AC 81 ms
76,476 KB
testcase_03 AC 65 ms
71,100 KB
testcase_04 AC 70 ms
71,068 KB
testcase_05 AC 67 ms
71,428 KB
testcase_06 AC 68 ms
71,380 KB
testcase_07 AC 64 ms
71,480 KB
testcase_08 AC 65 ms
71,380 KB
testcase_09 AC 66 ms
71,276 KB
testcase_10 AC 67 ms
71,104 KB
testcase_11 AC 67 ms
71,620 KB
testcase_12 AC 67 ms
71,304 KB
testcase_13 AC 89 ms
77,096 KB
testcase_14 AC 85 ms
76,388 KB
testcase_15 AC 160 ms
77,544 KB
testcase_16 AC 149 ms
77,720 KB
testcase_17 AC 214 ms
79,068 KB
testcase_18 AC 485 ms
97,716 KB
testcase_19 AC 502 ms
97,608 KB
testcase_20 AC 498 ms
97,132 KB
testcase_21 AC 497 ms
97,588 KB
testcase_22 AC 494 ms
97,472 KB
testcase_23 AC 68 ms
71,108 KB
testcase_24 AC 67 ms
71,444 KB
testcase_25 AC 65 ms
71,416 KB
testcase_26 AC 67 ms
71,476 KB
testcase_27 AC 67 ms
71,240 KB
testcase_28 AC 1,100 ms
155,636 KB
testcase_29 AC 1,115 ms
156,112 KB
testcase_30 AC 1,157 ms
157,008 KB
testcase_31 AC 1,132 ms
157,072 KB
testcase_32 AC 1,162 ms
156,808 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