結果

問題 No.157 2つの空洞
ユーザー satanicsatanic
提出日時 2016-05-28 01:40:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,645 bytes
コンパイル時間 1,661 ms
コンパイル使用メモリ 177,704 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-01 08:35:53
合計ジャッジ時間 2,564 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 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>
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define VAR(type, a) type a;std::cin>>a;
// VAR(int, x);
#define OUT(d) std::cout<<(d);
#define FOUT(n, d) std::cout<<std::fixed<<std::setprecision(n)<<(d);
#define SP std::cout<<" ";
#define TAB std::cout<<"\t";
#define BR std::cout<<"\n";
#define ENDL std::cout<<std::endl;
#define FLUSH std::cout<<std::flush;
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& r:c)for(auto& i:r)std::cin>>i;
#define ALL(a) (a).begin(),(a).end()
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define POS std::pair<int, int>
#define IN(a0, y, a1, b0, x, b1) (a0<=y && y<a1 && b0<=x && x<b1)
#define SHOW(d) {std::cout << #d << "\t:" << d << "\n";}
#define SHOWVECTOR(v) {std::cout << #v << "\t:";for(auto i : v){std::cout << i << " ";}std::cout << "\n";}
#define SHOWVECTOR2(v) {std::cout << #v << "\t:\n";for(auto i : v){for(auto j : i){std::cout << j.visited << " ";}std::cout << "\n";}}

//#define int ll
using ll = long long;
const int INF = 1<<30;

typedef struct{
    int y, x;
    bool visited;
}Pos;

std::array<int, 4> dy{{-1, 0, 1,  0}};
std::array<int, 4> dx{{ 0, 1, 0, -1}};

signed main(){
    //INIT;
    VAR(int, w)VAR(int, h);
    std::vector<std::vector<Pos>> P(h, std::vector<Pos>(w));
    REP(i, h)REP(j, w){
        char ch;
        std::cin >> ch;
        P[i][j].y = i;
        P[i][j].x = j;
        P[i][j].visited = ch=='#';
    }
    std::vector<std::vector<POS>> cave(2);
    std::stack<Pos> st;
    REP(ci, 2){
        REP(i, h){
            REP(j, w){
                if(!P[i][j].visited){
                    st.push(P[i][j]);
                    goto BFS;
                }
            }
        }
        BFS:
        while(!st.empty()){
            Pos t = st.top(); st.pop();
            Pos& now = P[t.y][t.x];
            now.visited = true;
            cave[ci].push_back(POS(now.y, now.x));
            REP(i, 4){
                Pos& check = P[now.y+dy[i]][now.x+dx[i]];
                if(!check.visited){
                    check.visited = true;
                    st.push(check);
                }
            }
        }
    }
    int min = 40;
    for(auto& p0 : cave[0]){
        for(auto& p1 : cave[1]){
            min = std::min(min, static_cast<int>(std::fabs(p0.first-p1.first)+std::fabs(p0.second-p1.second)));
        }
    }
    OUT(min-1)BR;       
    return 0;
}
0