結果

問題 No.971 いたずらっ子
コンテスト
ユーザー Kodai
提出日時 2020-04-27 01:49:33
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
TLE  
実行時間 -
コード長 955 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 394 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 392,448 KB
最終ジャッジ日時 2026-05-13 15:25:11
合計ジャッジ時間 7,287 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
sys.setrecursionlimit(10**6)
H, W = list(map(int, input().split()))
a_list = [[j for j in input()] for i in range(H)]


dp_dict = {}


def solve(h, w):
    if h == H - 1 and w == W - 1:
        return 0

    if (h, w) not in dp_dict:
        if h == H - 1:
            temp = solve(h, w + 1) + 2 + h + w if a_list[h][w + 1] == "k" else solve(h, w + 1) + 1
            dp_dict[(h, w)] = temp
            return temp
        elif w == W - 1:
            temp = solve(h + 1, w) + 2 + h + w if a_list[h + 1][w] == "k" else solve(h + 1, w) + 1
            dp_dict[(h, w)] = temp
            return temp
        else:
            temp = min([solve(h + 1, w) + 2 + h + w if a_list[h + 1][w] == "k" else solve(h + 1, w) + 1,
                        solve(h, w + 1) + 2 + h + w if a_list[h][w + 1] == "k" else solve(h, w + 1) + 1])
            dp_dict[(h, w)] = temp
            return temp
    else:
        return dp_dict[(h, w)]


print(solve(0, 0))
0