結果

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

テストケース

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

ソースコード

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 << endl;


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

    return 0;
}
0