結果

問題 No.13 囲みたい!
ユーザー ei1333333ei1333333
提出日時 2018-11-22 01:02:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 872 bytes
コンパイル時間 1,894 ms
コンパイル使用メモリ 198,696 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 02:44:59
合計ジャッジ時間 2,905 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

const int vy[] = {-1, 0, 1, 0};
const int vx[] = {0, 1, 0, -1};
int W, H, M[100][100];
int used[100][100];

bool is_in(int y, int x) {
  return 0 <= y && y < H && 0 <= x && x < W;
}

void dfs(int y, int x, int pre) {
  used[y][x] = true;
  for(int k = 0; k < 4; k++) {
    int ny = y + vy[k], nx = x + vx[k];
    if(!is_in(ny, nx)) continue;
    if(M[y][x] != M[ny][nx]) continue;
    if(k == pre) continue;
    if(used[ny][nx]) {
      cout << "possible\n";
      exit(0);
    }
    dfs(ny, nx, (k + 2) % 4);
  }
}

int main() {
  cin >> W >> H;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      cin >> M[i][j];
    }
  }
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      if(used[i][j]) continue;
      dfs(i, j, -1);
    }
  }
  cout << "impossible\n";
}

0