結果

問題 No.13 囲みたい!
ユーザー ninoinuininoinui
提出日時 2020-07-21 14:12:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,050 bytes
コンパイル時間 2,189 ms
コンパイル使用メモリ 210,644 KB
実行使用メモリ 812,816 KB
最終ジャッジ日時 2023-08-28 13:05:29
合計ジャッジ時間 5,413 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

vector<vector<int>> G;

void dfs(int v, int pre, int s) {
  for (auto next : G.at(v)) {
    if (next == pre) continue;
    if (next == s) {
      cout << "possible" << "\n";
      exit(0);
    }
    dfs(next, v, s);
  } 
}

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);
      }
    }
  }
  for (int i = 0; i < N; i++) dfs(i, -1, i);
  cout << "impossible" << "\n";
}
0