結果

問題 No.157 2つの空洞
ユーザー ryosuke000010ryosuke000010
提出日時 2018-05-09 20:26:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,538 bytes
コンパイル時間 560 ms
コンパイル使用メモリ 65,228 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 11:28:44
合計ジャッジ時間 1,611 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <math.h>
#include <vector>
using namespace std;

const int MAX_W = 20;
const int MAX_H = 20;
int W,H;
int ap=0,bp=0;
vector<string> field;
int Ax[MAX_W*MAX_H],Ay[MAX_W*MAX_H],Bx[MAX_W*MAX_H],By[MAX_W*MAX_H];
int dy[] = {1, 0, -1, 0};
int dx[] = { 0, 1, 0, -1 };

void dsf(int x,int y,int count)
{   
    if(count==0) 
    {
        field[x][y] = 'A';
        Ax[ap] = x, Ay[ap] = y;
        ap++;
    }
    else 
    {
        field[x][y] = 'B';
        Bx[bp] = x, By[bp] = y;
        bp++;
    }
    for(int i=0;i<4;i++)
    {   
        int nx = x + dx[i], ny = y + dy[i];
        if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny]=='.')
        {
            dsf(nx,ny,count);
        }
    }
    return ;
}
void solve()
{
    int min = W*H;
    int d;
    int count = 0;
    for(int h=0;h<H;h++)
    {
        for(int w=0;w<W;w++)
        {
            if(field[h][w]=='.')
            {  
                dsf(h,w,count);
                count++;
            }
            
        }
    }
    for(int i=0;i<ap;i++)
    {
        for(int j=0;j<bp;j++)
        {   
            d = abs(Ax[i]-Bx[j])+abs(Ay[i]-By[j])-1;
            if(d<min)
            {
                min = d;
            }
        }
    }
    cout << min << endl;
}

int main()
{
    cin >> W >> H;
	for(int i=0;i<H;i++)
    {
        string tmp;
        cin >> tmp;
        field.push_back(tmp);
	}
    solve();
    //for(int i=0;i<H;i++)
    //{
    //    cout << field[i] << endl;
    //}
    return 0;
}
0