結果

問題 No.157 2つの空洞
ユーザー machymachy
提出日時 2015-06-13 13:49:03
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,305 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 77,520 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-06 16:08:19
合計ジャッジ時間 1,320 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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