結果

問題 No.402 最も海から遠い場所
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-23 00:29:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 658 ms / 3,000 ms
コード長 1,872 bytes
コンパイル時間 677 ms
コンパイル使用メモリ 65,892 KB
実行使用メモリ 159,348 KB
最終ジャッジ日時 2024-04-24 03:18:26
合計ジャッジ時間 4,349 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
40,960 KB
testcase_01 AC 31 ms
40,960 KB
testcase_02 AC 33 ms
40,960 KB
testcase_03 AC 33 ms
40,960 KB
testcase_04 AC 32 ms
40,960 KB
testcase_05 AC 33 ms
40,960 KB
testcase_06 AC 32 ms
40,960 KB
testcase_07 AC 32 ms
40,832 KB
testcase_08 AC 32 ms
40,960 KB
testcase_09 AC 32 ms
40,832 KB
testcase_10 AC 33 ms
40,960 KB
testcase_11 AC 33 ms
40,960 KB
testcase_12 AC 33 ms
40,960 KB
testcase_13 AC 37 ms
41,216 KB
testcase_14 AC 34 ms
41,088 KB
testcase_15 AC 54 ms
42,752 KB
testcase_16 AC 49 ms
43,520 KB
testcase_17 AC 400 ms
72,064 KB
testcase_18 AC 658 ms
56,484 KB
testcase_19 AC 410 ms
159,348 KB
testcase_20 AC 75 ms
50,432 KB
testcase_21 AC 378 ms
105,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct State {
    int row, col, dist;

    State(int row, int col, int dist) : row(row), col(col), dist(dist) { }
};

const int kINF = 1 << 28;
const int kMAX_H = 3100;
const int kMAX_W = 3100;
string S[kMAX_H];

int dist[kMAX_H][kMAX_W];

int H, W;

bool IsIn(int row, int col) {
    return 0 <= row && row < H && 0 <= col && col < W;
}

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

    cin >> H >> W;
    for (int r = 0; r < H; r++) {
        cin >> S[r];
    }

    queue<State> que;
    fill((int* )dist, (int* )(dist + kMAX_H), kINF);
    for (int r = 0; r < H; r++) {
        for (int c = 0; c < W; c++) {
            if (S[r][c] == '.') {
                que.push(State(r, c, 0));
                dist[r][c] = 0;
            }
        }
    }
    while (!que.empty()) {
        State s = que.front(); que.pop();
        for (int dr = -1; dr <= 1; dr++) {
            for (int dc = -1; dc <= 1; dc++) {
                int nr = s.row + dr, nc = s.col + dc;
                if (IsIn(nr, nc) && S[nr][nc] == '#' && dist[nr][nc] == kINF) {
                    que.push(State(nr, nc, s.dist + 1));
                    dist[nr][nc] = s.dist + 1;
                }
            }
        }
    }

    int ans = 0;
    for (int r = 0; r < H; r++) {
        for (int c = 0; c < W; c++) {
            if (S[r][c] == '.') continue;
            /*
            if (S[r][c] == '.') {
                cout << "x ";
                continue;
            }
            */
            int move_r = min(r, H - 1 - r) + 1,
                move_c = min(c, W - 1 - c) + 1,
                c_dist = min(move_r, move_c);
            ans = max(ans, min(dist[r][c], c_dist));
            // cout << min(dist[r][c], c_dist) << " ";
        }
        // cout << endl;
    }
    cout << ans << endl;

    return 0;
}
0