結果

問題 No.157 2つの空洞
ユーザー peroonperoon
提出日時 2019-05-31 09:16:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,041 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 180,836 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 18:57:17
合計ジャッジ時間 3,238 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

struct Point{
    ll x;
    ll y;
    Point(ll x, ll y): x(x), y(y) {}
    Point(){}

    bool operator<(const Point &another) const
    {
        if(x == another.x){
            return y < another.y;
        }else{
            return x < another.x;
        }
    }
};

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

vector<string> S;
ll W, H;

void bfs(ll x, ll y, char c){
    queue<Point> que;
    que.push(Point(x, y));
    S[y][x] = c;

    while(!que.empty()){
        auto p = que.front();
        que.pop();

        FOR(i, 0, 4){
            ll tx = p.x + dx[i];
            ll ty = p.y + dy[i];
            if(0<=tx && tx<W && 0<=ty && ty<H && S[ty][tx]=='.'){
                S[ty][tx] = c;
                que.push(Point(tx, ty));
            }
        }
    }
}

ll manhattan_distance(Point a, Point b){
    return abs(a.x - b.x) + abs(a.y - b.y);
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    cin >> W >> H;
    S.resize(H);

    FOR(i, 0, H){
        cin >> S[i];
    }

    // 空洞を見つけてRに塗る
    FOR(y, 0, H){
        FOR(x, 0, W){
            if(S[y][x]=='.'){
                bfs(x, y, 'R');
                goto HERE;
            }
        }
    }
HERE:

    // 空洞を見つけてGに塗る
    FOR(y, 0, H){
        FOR(x, 0, W){
            if(S[y][x]=='.'){
                bfs(x, y, 'G');
                goto THERE;
            }
        }
    }
THERE:

    set<Point> se_R;
    set<Point> se_G;

    // Rに隣接する壁をse_Rに入れる
    // Gも同様
    // それは掘る始点となる
    FOR(y, 0, H){
        FOR(x, 0, W){
            FOR(i, 0, 4){
                ll tx = x + dx[i];
                ll ty = y + dy[i];
                if(0<=tx && tx<W && 0<=ty && ty<H && S[ty][tx]=='R'){
                    se_R.insert(Point(x, y));
                }
                if(0<=tx && tx<W && 0<=ty && ty<H && S[ty][tx]=='G'){
                    se_G.insert(Point(x, y));
                }
            }
        }
    }

    // Rの始点、Gの始点の各組み合わせのマンハッタン距離を求め、それらの内の最短距離を求める
    ll min_md = inf;
    for(Point p0 : se_R){
        for(Point p1 : se_G){
            ll md = manhattan_distance(p0, p1);
            chmin(min_md, md);
        }
    }

    p(min_md + 1);
    
    return 0;
}
0