結果

問題 No.157 2つの空洞
ユーザー dnishdnish
提出日時 2018-06-08 11:27:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,219 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 90,292 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-12 23:15:42
合計ジャッジ時間 1,763 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define p(s) cout<<(s)<<endl
#define REP(i,n,N) for(int i=n;i<N;i++)
#define RREP(i,n,N) for(int i=N-1;i>=n;i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf=1e9+7;

int H,W;
string s[21];
bool ok;
int f[21][21];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
vector<pair<int,int>> vt1, vt2;
int main(){
    cin>>W>>H;
    REP(i,0,H){
        cin>>s[i];
    }
    queue<pair<int,int>> q;
    REP(i,0,H) {
        REP(j, 0, W) {
            if (s[i][j] == '.') {
                f[i][j] = 1;
                vt1.push_back({i, j});
                q.push({i, j});
                break;
            }
        }
        if(!q.empty()) break;
    }
    while(!q.empty()){
        auto now = q.front(); q.pop();
        int i=now.F, j = now.S;
        REP(k,0,4){
            if(!CK(i+dy[k],0,H) || !CK(j+dx[k],0,W) ) continue;
            if(s[i+dy[k]][j+dx[k]]=='.'&&f[i+dy[k]][j+dx[k]]==0){
                f[i+dy[k]][j+dx[k]] = 1;
                vt1.push_back({i+dy[k], j+dx[k]});
                q.push({i+dy[k], j+dx[k]});
            }
        }
    }
    REP(i,0,H) {
        REP(j, 0, W) {
            if (s[i][j] == '.'&&f[i][j]==0) {
                f[i][j] = 1;
                vt2.push_back({i, j});
                q.push({i, j});
                break;
            }
        }
        if(!q.empty()) break;
    }
    while(!q.empty()){
        auto now = q.front(); q.pop();
        int i=now.F, j = now.S;
        REP(k,0,4){
            if(!CK(i+dy[k],0,H) || !CK(j+dx[k],0,W) ) continue;
            if(s[i+dy[k]][j+dx[k]]=='.'&&f[i+dy[k]][j+dx[k]]==0){
                f[i+dy[k]][j+dx[k]] = 1;
                vt2.push_back({i+dy[k], j+dx[k]});
                q.push({i+dy[k], j+dx[k]});
            }
        }
    }

    int ans = inf;
    for(auto v1:vt1){
        for(auto v2:vt2){
            ans = min(ans, abs(v2.F-v1.F)+abs(v1.S-v2.S)-1);
        }
    }
    p(ans);
    return 0;
}
0