結果

問題 No.157 2つの空洞
ユーザー aaaaaaiuaaaaaaiu
提出日時 2019-08-07 23:22:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,263 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 199,120 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 23:19:10
合計ジャッジ時間 3,620 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

void dfs(int y, int x, char c, string s[]) {
    s[y][x] = c;
    for (int i=0;i<4;i++) {
        int ny=y+dy[i];
        int nx=x+dx[i];
        if (s[ny][nx]=='.') dfs(ny,nx,c,s);
    }
}

int main() {
    int w,h;
    cin>>w>>h;
    string s[h];
    for (int i=0;i<h;i++)cin>>s[i];
    for (int i=0;i<h;i++) {
        int flag=0;
        for (int j=0;j<w;j++) {
            if (s[i][j]=='.') {
                dfs(i,j,'a',s);
                flag=1;
                break;
            }
        }
        if (flag) break;
    }
    for (int i=0;i<h;i++) {
        int flag=0;
        for (int j=0;j<w;j++) {
            if (s[i][j]=='.') {
                dfs(i,j,'b',s);
                flag=1;
                break;
            }
        }
        if (flag) break;
    }
    int ans=40;
    for (int i=0;i<h;i++) {
        for (int j=0;j<w;j++) {
            for (int k=0;k<h;k++) {
                for (int l=0;l<w;l++) {
                    if (s[i][j]=='a'&&s[k][l]=='b') {
                        ans=min(ans,abs(i-k)+abs(j-l)-1);
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
0