結果
問題 |
No.402 最も海から遠い場所
|
ユーザー |
|
提出日時 | 2021-02-16 22:45:05 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 573 ms / 3,000 ms |
コード長 | 2,116 bytes |
コンパイル時間 | 1,331 ms |
コンパイル使用メモリ | 141,800 KB |
最終ジャッジ日時 | 2025-01-18 21:33:48 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#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; }