結果

問題 No.157 2つの空洞
ユーザー 0w10w1
提出日時 2016-11-24 12:07:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,055 bytes
コンパイル時間 2,040 ms
コンパイル使用メモリ 184,060 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 06:57:42
合計ジャッジ時間 3,103 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

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

int in_range( int h, int w, int x, int y ){
  return 0 <= x and x < h and 0 <= y and y < w;
}

signed main(){
  int W, H; cin >> W >> H;
  vector< string > G( H );
  for( int i = 0; i < H; ++i )
    cin >> G[ i ];
  for( int i = 0; i < H; ++i )
    for( int j = 0; j < W; ++j )
      if( G[ i ][ j ] == '.' ){
        queue< pair< int, int > > que;
        vector< vector< int > > vis( H, vector< int >( W ) );
        que.emplace( i, j );
        vis[ i ][ j ] = 1;
        while( not que.empty() ){
          int x, y; tie( x, y ) = que.front(); que.pop();
          for( int di = 0; di < 4; ++di ){
            int nx = x + dx[ di ];
            int ny = y + dy[ di ];
            if( not in_range( H, W, nx, ny ) ) continue;
            if( vis[ nx ][ ny ] ) continue;
            if( G[ nx ][ ny ] == '#' ) continue;
            vis[ nx ][ ny ] = 1;
            que.emplace( nx, ny );
          }
        }
        for( int ni = 0; ni < H; ++ni )
          for( int nj = 0; nj < W; ++nj )
            if( G[ ni ][ nj ] == '.' and not vis[ ni ][ nj ] ){
              deque< tuple< int, int, int > > qued;
              qued.emplace_back( 0, ni, nj );
              vis[ ni ][ nj ] = 2;
              while( not qued.empty() ){
                int d, x, y; tie( d, x, y ) = qued.front(); qued.pop_front();
                for( int di = 0; di < 4; ++di ){
                  int nx = x + dx[ di ];
                  int ny = y + dy[ di ];
                  if( not in_range( H, W, nx, ny ) ) continue;
                  if( vis[ nx ][ ny ] == 2 ) continue;
                  if( vis[ nx ][ ny ] == 1 )
                    cout << d << endl, exit( 0 );
                  vis[ nx ][ ny ] = 2;
                  if( G[ nx ][ ny ] == '.' )
                    qued.emplace_front( d, nx, ny );
                  else
                    qued.emplace_back( d + 1, nx, ny );
                }
              }
            }
      }
  return 0;
}
0