結果

問題 No.971 いたずらっ子
ユーザー mikianaaamikianaaa
提出日時 2020-10-02 20:25:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,673 bytes
コンパイル時間 1,739 ms
コンパイル使用メモリ 176,940 KB
実行使用メモリ 227,672 KB
最終ジャッジ日時 2023-09-23 02:52:41
合計ジャッジ時間 10,229 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int H, W;
    cin >> H >> W;
    vector<vector<char>> G(H, vector<char>(W));
    rep(i, H) {
        rep(j, W) { cin >> G[i][j]; }
    }

    vector<vector<ll>> dist(H, vector<ll>(W, INF)),
        dist1(H, vector<ll>(W, INF));
    queue<pint> que;

    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    dist[0][0] = 0;
    dist1[0][0] = 0;
    que.push({0, 0});

    while (!que.empty()) {
        pll v = que.front();
        int y = v.first, x = v.second;
        que.pop();

        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (nx < 0 || ny < 0 || ny >= H || nx >= W)
                continue;
            if (G[ny][nx] == 'k') {
                if (dist[ny][nx] < dist[y][x] + dist1[y][x] + 2)
                    continue;
                else {
                    dist[ny][nx] = dist[y][x] + dist1[y][x] + 2;
                    dist1[ny][nx] = dist1[y][x] + 1;
                }
            } else {
                if (dist[ny][nx] < dist[y][x] + 1)
                    continue;
                else {
                    dist[ny][nx] = dist[y][x] + 1;
                    dist1[ny][nx] = dist1[y][x] + 1;
                }
            }
            que.push({ny, nx});
        }
    }

    cout << dist[H - 1][W - 1] << endl;
}
0