結果

問題 No.157 2つの空洞
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-30 23:42:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,157 bytes
コンパイル時間 1,442 ms
コンパイル使用メモリ 166,736 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-08 13:41:45
合計ジャッジ時間 2,438 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int W, H;
string C[30];

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

void f(int x, int y) {
    if (x < 0 || y < 0 || x >= H || y >= W)
        return;
    if (C[x][y] == '.') {
        C[x][y] = '?';
        for (int i = 0; i < 4; i++) {
            f(x + dx[i], y + dy[i]);
        }
    }
}


int main() {
    cin >> W >> H;
    for (int i = 0; i < H; i++) {
        cin >> C[i];
    }

    bool flag = false;
    for (int x = 0; x < H; x++) {
        for (int y = 0; y < W; y++) {
            if (C[x][y] == '.') {
                flag = true;
                f(x, y);
            }
            if (flag)
                break;
        }
        if (flag)
            break;
    }


    int ans = 100;
    for (int x1 = 0; x1 < H; x1++) {
        for (int x2 = 0; x2 < H; x2++) {
            for (int y1 = 0; y1 < W; y1++) {
                for (int y2 = 0; y2 < W; y2++) {
                    if (C[x1][y1] == '.' && C[x2][y2] == '?')
                        ans = min(ans, abs(x1 - x2) + abs(y1 - y2) - 1);
                }
            }
        }
    }

    cout << ans << endl;
}
0