結果

問題 No.157 2つの空洞
ユーザー tetsuzuki1115tetsuzuki1115
提出日時 2018-02-25 15:36:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,518 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 96,268 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 15:24:39
合計ジャッジ時間 1,831 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <cmath>
#include <limits.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

long long MOD = 1000000007;

struct Point {
    Point(){};
    Point( int a, int b ) { x = a; y = b; }
    int x;
    int y;
};

int dist( Point p1, Point p2 ) {
    return abs(p1.x-p2.x) + abs(p1.y-p2.y);
}

vector<string> V;
vector< vector<Point> > P(2);

int W,H;
int dir[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };
bool isOK( int x, int y ) {
    return x >= 0 && y >= 0 && x < W && y < H;
}

void func( int a, int x, int y ) {
    // cout << a << " " << x << " " << y << endl;
    Point p(x,y);
    P[a].push_back(p);
    V[y][x] = '#';
    for ( int i = 0; i < 4; i++ ) {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if ( isOK(nx,ny) && V[ny][nx] != '#' ) {
            func(a,nx,ny);
        }
    }
}

int main() {
    cin >> W >> H;
    
    V.resize(H);
    for ( int i = 0; i < H; i++ ) {
        cin >> V[i];
    }
    int a = 0;
    for ( int y = 0; y < H; y++ ) {
    for ( int x = 0; x < W; x++ ) {
        if ( V[y][x] != '#' ) {
            func(a,x,y);
            a++;
        }
    }
    }
    int ans = INT_MAX;
    for ( int i = 0; i < P[0].size(); i++ ) {
    for ( int j = 0; j < P[1].size(); j++ ) {
        ans = min( ans, dist( P[0][i], P[1][j] )-1 );
    }
    }
    cout << ans << endl;

    return 0;
}
0