結果

問題 No.13 囲みたい!
ユーザー 0w10w1
提出日時 2016-12-04 00:04:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,233 bytes
コンパイル時間 1,661 ms
コンパイル使用メモリ 170,780 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 10:30:30
合計ジャッジ時間 2,390 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

const string msg[] = { "impossible", "possible" };
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;
}

int dfs( const vector< vector< int > > &G, vector< vector< int > > &vis, int x, int y, int px, int py ){
  vis[ x ][ y ] = G[ x ][ y ];
  for( int di = 0; di < 4; ++di ){
    int nx = x + dx[ di ];
    int ny = y + dy[ di ];
    if( not in_range( G.size(), G[ 0 ].size(), nx, ny ) ) continue;
    if( G[ nx ][ ny ] != G[ x ][ y ] ) continue;
    if( nx == px and ny == py ) continue; 
    if( vis[ nx ][ ny ] == G[ x ][ y ] )
      return 1;
    if( dfs( G, vis, nx, ny, x, y ) )
      return 1;
  }
  return 0;
}

signed main(){
  int H, W; cin >> W >> H;
  vector< vector< int > > M( H, vector< int >( W ) );
  for( int i = 0; i < H; ++i )
    for( int j = 0; j < W; ++j )
      cin >> M[ i ][ j ];
  vector< vector< int > > vis( H, vector< int >( W ) );
  for( int i = 0; i < H; ++i )
    for( int j = 0; j < W; ++j ) if( not vis[ i ][ j ] )
      if( dfs( M, vis, i, j, -1, -1 ) )
        cout << msg[ 1 ] << endl, exit( 0 );
  cout << msg[ 0 ] << endl;
  return 0;
}
0