結果

問題 No.402 最も海から遠い場所
ユーザー haruteru
提出日時 2016-07-26 13:10:29
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1,091 ms / 3,000 ms
コード長 2,373 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 58,580 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-11-06 16:56:27
合計ジャッジ時間 6,879 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
using namespace std;

#define MAX (3000)

int H, W;
int S[MAX+2][MAX+2];

int fn(int h, int w, int (*inc)(int &,int &))
{
    int *p = &S[h][w];
    if (inc(h, w)) {
        *p = min(*p, 1);
    }
    else {
        *p = min(*p, fn(h, w, inc) + 1);
    }
    return *p;
}

int main()
{
    cin >> H;
    cin >> W;
    for (int h = 0; h < H; h++) {
        string s;
        cin >> s;
        for (int w = 0; w < W; w++) {
            if (s[w] == '.') {
                S[h][w] = 0;
            }
            else {
                S[h][w] = MAX;
            }
        }
    }
    for (int h = 0; h < H; h++) {
        fn(h, 0, [](int &hh, int &ww)->int {
            if (++ww >= W) return -1;
            return 0;
        });
        fn(h, W-1, [](int &hh, int &ww)->int {
            if (--ww < 0) return -1;
            return 0;
        });
        fn(h, 0, [](int &hh, int &ww)->int {
            if (++hh >= H || ++ww >= W) return -1;
            return 0;
        });
        fn(h, 0, [](int &hh, int &ww)->int {
            if (--hh < 0 || ++ww >= W) return -1;
            return 0;
        });
        fn(h, W-1, [](int &hh, int &ww)->int {
            if (--hh < 0 || --ww < 0) return -1;
            return 0;
        });
        fn(h, W-1, [](int &hh, int &ww)->int {
            if (++hh >= H || --ww < 0) return -1;
            return 0;
        });
    }
    for (int w = 0; w < W; w++) {
        fn(0, w, [](int &hh, int &ww)->int {
            if (++hh >= H) return -1;
            return 0;
        });
        fn(H-1, w, [](int &hh, int &ww)->int {
            if (--hh < 0) return -1;
            return 0;
        });
        fn(0, w, [](int &hh, int &ww)->int {
            if (++hh >= H || ++ww >= W) return -1;
            return 0;
        });
        fn(0, w, [](int &hh, int &ww)->int {
            if (++hh >= H || --ww < 0) return -1;
            return 0;
        });
        fn(H-1, w, [](int &hh, int &ww)->int {
            if (--hh < 0 || ++ww >= W) return -1;
            return 0;
        });
        fn(H-1, w, [](int &hh, int &ww)->int {
            if (--hh < 0 || --ww < 0) return -1;
            return 0;
        });
    }
    int ans = 0;
    for (int h = 0; h < H; h++) {
        for (int w = 0; w < W; w++) {
            ans = max(ans, S[h][w]);
        }
    }
    cout << ans << endl;
}
0