結果

問題 No.402 最も海から遠い場所
ユーザー kimiyukikimiyuki
提出日時 2016-07-22 22:58:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,035 ms / 3,000 ms
コード長 1,622 bytes
コンパイル時間 626 ms
コンパイル使用メモリ 65,816 KB
実行使用メモリ 112,764 KB
最終ジャッジ日時 2023-08-06 09:56:38
合計ジャッジ時間 6,028 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 26 ms
4,952 KB
testcase_16 AC 35 ms
5,644 KB
testcase_17 AC 460 ms
38,396 KB
testcase_18 AC 1,035 ms
43,240 KB
testcase_19 AC 828 ms
112,764 KB
testcase_20 AC 908 ms
39,636 KB
testcase_21 AC 799 ms
76,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <queue>
#include <tuple>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
template <typename T, typename X> auto vectors(T a, X x) { return vector<T>(x, a); }
template <typename T, typename X, typename Y, typename... Zs> auto vectors(T a, X x, Y y, Zs... zs) { auto cont = vectors(a, y, zs...); return vector<decltype(cont)>(x, cont); }
int main() {
    // input
    int h, w; scanf("%d%d", &h, &w);
    vector<vector<bool> > is_sea = vectors<bool>(false, h+2, w+2);
    repeat_from (y,1,h+1) {
        repeat_from (x,1,w+1) {
            char c; scanf(" %c", &c);
            is_sea[y][x] = c == '#';
        }
    }
    // compute
    vector<vector<int> > dist = vectors<int>(-1, h+2, w+2);
    queue<pair<int,int> > que;
    repeat (y,h+2) {
        repeat (x,w+2) {
            if (not is_sea[y][x]) {
                dist[y][x] = 0;
                que.emplace(y, x);
            }
        }
    }
    int ans = -1;
    while (not que.empty()) {
        int y, x; tie(y, x) = que.front(); que.pop();
        ans = dist[y][x];
        for (int dy : { -1, 0, 1 }) {
            for (int dx : { -1, 0, 1 }) {
                int ny = y + dy;
                int nx = x + dx;
                if (ny < 0 or h+2 <= ny or nx < 0 or w+2 <= nx) continue;
                if (dist[ny][nx] == -1) {
                    dist[ny][nx] = dist[y][x] + 1;
                    que.emplace(ny, nx);
                }
            }
        }
    }
    // output
    printf("%d\n", ans);
    return 0;
}
0