結果

問題 No.157 2つの空洞
ユーザー ry0u_ydry0u_yd
提出日時 2015-09-05 03:00:01
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,257 bytes
コンパイル時間 741 ms
コンパイル使用メモリ 82,180 KB
実行使用メモリ 597,556 KB
最終ジャッジ日時 2023-09-26 08:35:30
合計ジャッジ時間 5,211 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 22 ms
4,376 KB
testcase_07 AC 1 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 49 ms
4,380 KB
testcase_12 AC 374 ms
14,064 KB
testcase_13 AC 64 ms
5,104 KB
testcase_14 MLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int w,h,x,y;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
vector<string> s;

bool can(int y,int x) {
    if(0 <= y && y < h && 0 <= x && x < w) return true;
    return false;
}

bool used[25][25];
void dfs(int y,int x,vector<int>& v) {
    if(used[y][x]) return;
    used[y][x] = true;
    v.push_back(y*w + x);

    rep(i,4) {
        int ny = y + dy[i];
        int nx = x + dx[i];

        if(can(ny,nx) && s[ny][nx] == '.') {
            dfs(ny,nx,v);
        }
    }
}

int main() {
    cin >> w >> h;

    s.resize(h);
    rep(i,h) cin >> s[i];

    vector< vector<int> > a(2);
    memset(used,0,sizeof(used));

    int id = 0;
    rep(i,h) {
        rep(j,w) {
            if(used[i][j]) continue;
            if(s[i][j] == '.') {
                dfs(i,j,a[id]);
                id++;
            }
        }
    }
    
    int ans = INF;
    rep(i,a[0].size()) {
        rep(j,a[1].size()) {
            queue<P> que;
            que.push(P(a[0][i],0));
            memset(used,0,sizeof(used));

            while(que.size()) {
                P p = que.front();
                que.pop();

                if(p.first == a[1][j]) {
                    ans = min(ans,p.second);
                    break;
                }

                int y = p.first / w;
                int x = p.first % w;

                used[y][x] = true;
                rep(k,4) {
                    int ny = y + dy[k];
                    int nx = x + dx[k];

                    if(can(ny,nx) && !used[ny][nx]) {
                        if(s[ny][nx] == '.') {
                            que.push(P(ny*w+nx,p.second));
                        }
                        if(s[ny][nx] == '#') {
                            que.push(P(ny*w+nx,p.second+1));
                        }
                    }
                }
            }
        }
    }

    cout << ans << endl;


    return 0;
}
0