結果

問題 No.402 最も海から遠い場所
ユーザー outlineoutline
提出日時 2021-02-16 22:45:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 721 ms / 3,000 ms
コード長 2,116 bytes
コンパイル時間 1,665 ms
コンパイル使用メモリ 146,216 KB
実行使用メモリ 120,964 KB
最終ジャッジ日時 2023-10-11 12:28:54
合計ジャッジ時間 6,854 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,356 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 4 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 16 ms
5,560 KB
testcase_16 AC 21 ms
6,280 KB
testcase_17 AC 271 ms
42,540 KB
testcase_18 AC 721 ms
51,468 KB
testcase_19 AC 453 ms
120,964 KB
testcase_20 AC 516 ms
47,724 KB
testcase_21 AC 424 ms
84,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

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

    int H, W;
    cin >> H >> W;
    vector<vector<char>> S(H + 2, vector<char>(W + 2, '.'));
    for(int i = 1; i <= H; ++i){
        for(int j = 1; j <= W; ++j){
            cin >> S[i][j];
        }
    }
    vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
    queue<pair<int, int>> que;
    for(int x = 0; x <= H + 1; ++x){
        for(int y = 0; y <= W + 1; ++y){
            if(S[x][y] == '.'){
                dist[x][y] = 0;
                que.emplace(x, y);
            }
        }
    }
    while(!que.empty()){
        auto [x, y] = que.front();
        que.pop();
        for(int i = 0; i < 8; ++i){
            int nx = x + dx[i], ny = y + dy[i];
            if(nx < 0 || nx >= H + 2 || ny < 0 || ny >= W + 2)  continue;
            if(chmin(dist[nx][ny], dist[x][y] + 1)){
                que.emplace(nx, ny);
            }
        }
    }
    int ans = 0;
    for(int x = 1; x <= H; ++x){
        for(int y = 1; y <= W; ++y){
            chmax(ans, dist[x][y]);
        }
    }
    cout << ans << endl;

    return 0;
}
0