結果

問題 No.13 囲みたい!
ユーザー AuroraAurora
提出日時 2022-05-08 13:27:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 640 ms / 5,000 ms
コード長 1,229 bytes
コンパイル時間 2,905 ms
コンパイル使用メモリ 179,632 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 09:02:14
合計ジャッジ時間 4,892 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

int main(){
  int W,H;
  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];
  bool flag = false;
  vector<int> dx = {1,0,0,-1};
  vector<int> dy = {0,1,-1,0};
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      vector<vector<bool>> seen(H,vector<bool>(W,false));
      seen[i][j] = true;
      int N = M[i][j];
      queue<pair<pair<int,int>,int>> que;
      que.push(make_pair(make_pair(i,j),-1));
      while(!que.empty()){
        int x = que.front().first.first;
        int y = que.front().first.second;
        int d = que.front().second;
        que.pop();
        for(int k=0;k<4;k++){
          int nx = x+dx[k];
          int ny = y+dy[k];
          if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
          if(k == 3-d) continue;
          if(M[nx][ny] != N) continue;
          if(seen[nx][ny]){
            flag = true;
            break;
          }
          else{
            seen[nx][ny] = true;
            que.push(make_pair(make_pair(nx,ny),k));
          }
        }
        if(flag) break;
      }
    }
  }
  if(flag) cout << "possible" << endl;
  else cout << "impossible" << endl;
}
0