結果
問題 |
No.157 2つの空洞
|
ユーザー |
![]() |
提出日時 | 2018-05-09 20:26:22 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,538 bytes |
コンパイル時間 | 582 ms |
コンパイル使用メモリ | 66,292 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-28 02:50:31 |
合計ジャッジ時間 | 1,276 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 16 |
ソースコード
#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; }