結果

問題 No.13 囲みたい!
ユーザー ninoinuininoinui
提出日時 2020-07-21 14:27:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,146 bytes
コンパイル時間 2,339 ms
コンパイル使用メモリ 212,212 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 13:35:51
合計ジャッジ時間 3,339 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

vector<vector<int>> G;

void dfs(int v, int pre, vector<bool> &seen) {
  if (seen.at(v)) return;
  else seen.at(v) = 1;
  for (auto next : G.at(v)) {
    if (next == pre) continue;
    if (seen.at(next)) {
      cout << "possible" << "\n";
      exit(0);
    }
    dfs(next, v, seen);
  } 
}

int main() {
  int W, H, N = 0;
  cin >> W >> H;
  vector<vector<pair<int, int>>> M(H, vector<pair<int, int>>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int m;
      cin >> m;
      M.at(i).at(j) = {m, N++};
    }
  }
  G.resize(N);
  vector<int> mh(4), mw(4);
  mh = {-1, 0, 1, 0};
  mw = {0, 1, 0, -1};
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      for (int i = 0; i < 4; i++) {
        int nh = h + mh.at(i);
        int nw = w + mw.at(i);
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        auto [a, b] = M.at(h).at(w);
        auto [c, d] = M.at(nh).at(nw);
        if (a == c) G.at(b).push_back(d);
      }
    }
  }
  vector<bool> seen(N);
  for (int i = 0; i < N; i++) dfs(i, -1, seen);
  cout << "impossible" << "\n";
}
0