結果

問題 No.971 いたずらっ子
ユーザー snrnsidysnrnsidy
提出日時 2021-05-26 13:28:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,052 bytes
コンパイル時間 2,147 ms
コンパイル使用メモリ 194,836 KB
実行使用メモリ 23,168 KB
最終ジャッジ日時 2024-04-25 02:17:52
合計ジャッジ時間 4,108 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
23,040 KB
testcase_01 AC 76 ms
23,168 KB
testcase_02 AC 62 ms
23,040 KB
testcase_03 AC 67 ms
23,040 KB
testcase_04 AC 65 ms
22,016 KB
testcase_05 AC 68 ms
22,912 KB
testcase_06 AC 56 ms
20,736 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 22 ms
13,440 KB
testcase_09 AC 22 ms
13,312 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 10 ms
15,488 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int H, W;
char board[2001][2001];
int dp[2001][2001];

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin >> H >> W;

    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            cin >> board[i][j];
        }
    }

    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            dp[i][j] = 1e9;
        }
    }

    dp[0][0] = 0;

    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            if (i == 0 && j == 0) continue;
            int cost = 0;
            if (board[i][j] == 'k')
            {
                cost += abs(i) + abs(j);
            }
            if (i - 1 >= 0)
            {
                dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1 + cost);
            }
            if (j - 1 >= 0)
            {
                dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1 + cost);
            }
        }
    }

    cout << dp[H - 1][W - 1] << '\n';

    return 0;
}
0