結果

問題 No.402 最も海から遠い場所
ユーザー imulanimulan
提出日時 2016-07-26 18:30:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 750 ms / 3,000 ms
コード長 1,821 bytes
コンパイル時間 1,606 ms
コンパイル使用メモリ 172,792 KB
実行使用メモリ 86,468 KB
最終ジャッジ日時 2023-08-06 14:03:53
合計ジャッジ時間 5,758 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
38,604 KB
testcase_01 AC 12 ms
38,488 KB
testcase_02 AC 12 ms
38,516 KB
testcase_03 AC 12 ms
38,720 KB
testcase_04 AC 12 ms
38,444 KB
testcase_05 AC 12 ms
38,440 KB
testcase_06 AC 12 ms
38,504 KB
testcase_07 AC 12 ms
38,744 KB
testcase_08 AC 12 ms
38,432 KB
testcase_09 AC 12 ms
38,444 KB
testcase_10 AC 12 ms
38,440 KB
testcase_11 AC 12 ms
38,732 KB
testcase_12 AC 12 ms
38,436 KB
testcase_13 AC 15 ms
38,700 KB
testcase_14 AC 14 ms
38,588 KB
testcase_15 AC 32 ms
39,688 KB
testcase_16 AC 32 ms
40,472 KB
testcase_17 AC 380 ms
62,976 KB
testcase_18 AC 750 ms
53,692 KB
testcase_19 AC 357 ms
49,896 KB
testcase_20 AC 499 ms
50,096 KB
testcase_21 AC 434 ms
86,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define x first
#define y second

typedef pair<int,int> pi;

int h,w;
int d[3000][3000];

//8近傍
int dx[8]={0,1,1,1,0,-1,-1,-1}, dy[8]={-1,-1,0,1,1,1,0,-1};

const int INF=12345678;

inline bool in(int x, int y)
{
    return (0<=x&&x<w && 0<=y&&y<h);
}

int main()
{
    cin >>h >>w;
    vector<string> s(h);
    rep(i,h) cin >>s[i];

    fill(d[0],d[3000],INF);

    //まず海からの距離が1のところを探す
    rep(i,h)rep(j,w)
    {
        //そこが海なら
        if(s[i][j]=='.')
        {
            d[i][j]=0;
            //8近傍にある陸の距離は1
            rep(k,8)
            {
                int nx=j+dx[k], ny=i+dy[k];
                if(in(nx,ny) && s[ny][nx]=='#') d[ny][nx]=1;
            }
        }
    }

    //外側は全て海なので,端にある陸も海との距離は1
    rep(i,h)
    {
        if(s[i][0]=='#') d[i][0]=1;
        if(s[i][w-1]=='#') d[i][w-1]=1;
    }
    rep(i,w)
    {
        if(s[0][i]=='#') d[0][i]=1;
        if(s[h-1][i]=='#') d[h-1][i]=1;
    }

    queue<pi> que;
    rep(i,h)rep(j,w) if(d[i][j]==1) que.push(pi(j,i));

    while(!que.empty())
    {
        pi now=que.front();
        que.pop();
        rep(i,8)
        {
            int nx=now.x+dx[i], ny=now.y+dy[i];
            if(in(nx,ny) && d[ny][nx]>d[now.y][now.x]+1)
            {
                d[ny][nx]=d[now.y][now.x]+1;
                que.push(pi(nx,ny));
            }
        }
    }

    int ans=0;
    rep(i,h)rep(j,w) ans=max(ans,d[i][j]);
    cout << ans << endl;
    return 0;
}
0