結果

問題 No.157 2つの空洞
ユーザー suakiisuakii
提出日時 2024-04-03 09:41:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,422 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 160,128 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 09:41:17
合計ジャッジ時間 2,439 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

int w, h;
char a[100][100];
int dx[]={1, 0, -1, 0};
int dy[]={0, 1, 0, -1};
int cnt = 1;

void dfs(int x, int y, int cnt) {
    a[x][y] = '0' + cnt;
    for (int i = 0; i < 4; i++) {
        if (a[x+dx[i]][y+dy[i]] == '.') {
            dfs(x+dx[i], y+dy[i], cnt);
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    //freopen("input.txt", "r", stdin);

    cin >> w >> h;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
        }
    }

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] == '.') {
                dfs(i, j, cnt);
                cnt = cnt + 1;
            }
        }
    }
    int ans = INT_MAX;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] == '1') {
                for(int k = 0; k < h; k++) {
                    for (int m = 0; m < w; m++) {
                        if (a[k][m] == '2') {
                            ans = min(ans, abs(i-k) + abs(j-m));
                        }
                    }
                }
            }
        }
    }
    cout << ans -1 << endl;


    // for (int i = 0; i < h; i++) {
    //     for (int j = 0; j < w; j++) {
    //         cout << a[i][j];
    //     }
    //     cout << "\n";
    // }

    return 0;
}
0