結果

問題 No.402 最も海から遠い場所
コンテスト
ユーザー himueu
提出日時 2026-10-17 22:54:34
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 431 ms / 3,000 ms
+ 622µs
コード長 1,418 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,312 ms
コンパイル使用メモリ 208,464 KB
実行使用メモリ 123,800 KB
最終ジャッジ日時 2026-10-17 22:54:48
合計ジャッジ時間 8,793 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

int main() {
    using namespace std;
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for (auto &e : s) { cin >> e; }

    vector dist(h + 2, vector<int>(w + 2, -1));
    queue<pair<int, int>> que;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == '.') {
                dist[i + 1][j + 1] = 0;
                que.emplace(i + 1, j + 1);
            }
        }
    }
    for (int i = 0; i < h + 2; i++) {
        dist[i][0] = 0;
        que.emplace(i, 0);
        dist[i][w + 1] = 0;
        que.emplace(i, w + 1);
    }
    for (int j = 0; j < w + 2; j++) {
        dist[0][j] = 0;
        que.emplace(0, j);
        dist[h + 1][j] = 0;
        que.emplace(h + 1, j);
    }

    constexpr int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    constexpr int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
    int ans = 0;
    while (!que.empty()) {
        auto [x, y] = que.front();
        que.pop();
        ans = max(ans, dist[x][y]);
        for (int i = 0; i < 8; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || nx > h + 1 || ny < 0 || ny > w + 1) { continue; }
            if (dist[nx][ny] == -1) {
                dist[nx][ny] = dist[x][y] + 1;
                que.emplace(nx, ny);
            }
        }
    }
    cout << ans << endl;
}
0