結果

問題 No.402 最も海から遠い場所
コンテスト
ユーザー h_noson
提出日時 2016-07-22 23:59:49
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 509 ms / 3,000 ms
コード長 1,848 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,445 ms
コンパイル使用メモリ 173,728 KB
実行使用メモリ 160,324 KB
最終ジャッジ日時 2026-03-08 16:06:58
合計ジャッジ時間 4,717 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <cassert>
using namespace std;

#define GET_ARG(a,b,c,F,...) F
#define REP3(i,s,e) for (i = s; i <= e; i++)
#define REP2(i,n) REP3 (i,0,(int)(n)-1)
#define REP(...) GET_ARG (__VA_ARGS__,REP3,REP2) (__VA_ARGS__)
#define RREP3(i,s,e) for (i = s; i >= e; i--)
#define RREP2(i,n) RREP3 (i,(int)(n)-1,0)
#define RREP(...) GET_ARG (__VA_ARGS__,RREP3,RREP2) (__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

typedef long long ll;

constexpr int INF = 1e8;
constexpr int MOD = 1e9+7;
constexpr int ESP = 1e-9;
constexpr double PI = acos(-1);

string s[3000];
int dist[3000][3000];
bool used[3000][3000];
int dxy[] = {0,1,-1,-1,0,-1,1,1};
int h, w;

int main(void) {
    int i, j;
    cin >> h >> w;
    REP (i,h) cin >> s[i];

    REP (i,h) REP (j,w) dist[i][j] = INF;
    queue<pair<int,pair<int,int>>> q;
    REP (i,h) REP (j,w) if (s[i][j] == '.') {
        q.push(make_pair(0,make_pair(i,j)));
        dist[i][j] = 0;
    }
    REP (i,h) REP (j,w) {
        if (s[i][j] == '#' && (i == 0 || i == h-1 || j == 0 || j == w-1)) {
            q.push(make_pair(1,make_pair(i,j)));
            dist[i][j] = 1;
        }
    }
    while (!q.empty()) {
        int d = q.front().first;
        int y = q.front().second.first;
        int x = q.front().second.second;
        q.pop();
        REP (i,8) {
            int ny = y + dxy[i];
            int nx = x + dxy[(i+1)%8];
            if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if (dist[ny][nx] > d + 1) {
                q.push(make_pair(d+1,make_pair(ny,nx)));
                dist[ny][nx] = d + 1;
            }
        }
    }
    int ans = 0;
    REP (i,h) REP (j,w) ans = max(ans,dist[i][j]);
    cout << ans << endl;
    return 0;
}
0