結果

問題 No.13 囲みたい!
ユーザー kk
提出日時 2021-02-19 22:49:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,038 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 199,856 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-15 03:51:51
合計ジャッジ時間 3,016 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

int w, h;
int bd[128][128];
int vis[128][128];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};

bool dfs(int y, int x, int py, int px) {
  if (vis[y][x] == -1)
    return true;
  
  vis[y][x] = -1;
  for (int k = 0; k < 4; k++) {
    int y2 = y + dy[k];
    int x2 = x + dx[k];
    if (x2 < 0 || x2 >= w) continue;
    if (y2 < 0 || y2 >= h) continue;
    if (y2 == py && x2 == px) continue;
    if (bd[y2][x2] != bd[y][x]) continue;
    if (dfs(y2, x2, y, x)) return true;
  }
  vis[y][x] = 1;
  return false;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> w >> h;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> bd[i][j];

  bool ck = false;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vis[i][j]) continue;
      ck |= dfs(i, j, -1, -1);
    }
  }
  

  if (ck)
    cout << "possible" << endl;
  else
    cout << "impossible" << endl;
  
  return 0;
}
0