結果

問題 No.157 2つの空洞
ユーザー machymachy
提出日時 2015-06-13 13:49:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,305 bytes
コンパイル時間 538 ms
コンパイル使用メモリ 76,796 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 21:20:36
合計ジャッジ時間 1,525 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
#include <map>
using namespace std;
typedef long long LL;

void dfs(vector<string>& board, int x, int y){
    if(board[y][x] != '.') return;
    board[y][x] = 'o';
    if(x-1 >= 0) dfs(board, x-1, y);
    if(x+1 < board[0].size()) dfs(board, x+1, y);
    if(y-1 >= 0) dfs(board, x, y-1);
    if(y+1 < board.size()) dfs(board, x, y+1);
}

int iabs(int n){
    if(n < 0) return -n;
    return n;
}

int main(){
    int W, H;
    cin >> W >> H;
    vector<string> board(H);
    for(int i = 0; i < H; i++){
        cin >> board[i];
    }
    for(int y = 0; y < H; y++){
        for(int x = 0; x < W; x++){
            if(board[y][x] == '.'){
                dfs(board, x, y);
                y = H;
                break;
            }
        }
    }
    int ans = H*W;
    for(int y = 0; y < H; y++){
        for(int x = 0; x < W; x++){
            if(board[y][x] != 'o') continue;
            for(int y2 = 0; y2 < H; y2++){
                for(int x2 = 0; x2 < W; x2++){
                    if(board[y2][x2] == '.'){
                        ans = min(ans, iabs(x-x2)+iabs(y-y2)-1);
                    }
                }
            }
        }
    }
    cout <<  ans << endl;
    return 0;
}
0