結果

問題 No.402 最も海から遠い場所
ユーザー xuzijian629xuzijian629
提出日時 2018-11-05 22:24:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 898 ms / 3,000 ms
コード長 1,583 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 208,356 KB
実行使用メモリ 147,184 KB
最終ジャッジ日時 2023-08-13 03:41:42
合計ジャッジ時間 6,797 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 21 ms
6,216 KB
testcase_16 AC 25 ms
7,472 KB
testcase_17 AC 366 ms
55,164 KB
testcase_18 AC 898 ms
77,680 KB
testcase_19 AC 582 ms
147,184 KB
testcase_20 AC 651 ms
74,008 KB
testcase_21 AC 533 ms
110,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    int h, w;
    cin >> h >> w;
    vvi b(h + 2, vi(w + 2));
    for (int i = 1; i < h + 1; i++) {
        for (int j = 1; j < w + 1; j++) {
            b[i][j] = 1e9;
        }
    }
    using ii = pair<int, int>;
    queue<ii> que;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            char c;
            cin >> c;
            if (c == '.') {
                b[i + 1][j + 1] = 0;
            }
        }
    }

    for (int i = 0; i < h + 2; i++) {
        for (int j = 0; j < w + 2; j++) {
            if (b[i][j] == 0) {
                que.push(ii(i, j));
            }
        }
    }
    
    int dx[] = {1, 1, 1, 0, -1, -1, -1, 0};
    int dy[] = {1, 0, -1, -1, -1, 0, 1, 1};

    auto ok = [&](int x, int y) {
        return 0 <= x && x < h + 2 && 0 <= y && y < w + 2;
    };

    while (que.size()) {
        ii t = que.front();
        que.pop();
        for (int i = 0; i < 8; i++) {
            int x = t.first + dx[i];
            int y = t.second + dy[i];

            if (ok(x, y) && b[t.first][t.second] + 1 < b[x][y]) {
                b[x][y] = b[t.first][t.second] + 1;
                que.push(ii(x, y));
            }
        }
    }

    i64 nax = -1;
    for (int i = 0; i < h + 2; i++) {
        for (int j = 0; j < w + 2; j++) {
            nax = max(nax, b[i][j]);
        }
    }
    
    cout << nax << endl;
}
0