結果

問題 No.13 囲みたい!
ユーザー 久我山菜々久我山菜々
提出日時 2019-02-09 04:26:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,611 bytes
コンパイル時間 896 ms
コンパイル使用メモリ 95,560 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 04:05:24
合計ジャッジ時間 1,787 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <stack>

int main(int, char**) {
  int W, H;
  std::cin >> W >> H;

  std::vector<std::vector<int>> vec(H, std::vector(W, 0));
  std::vector<std::vector<int>> map(H, std::vector(W, 0));
  for(int i{}; i < H; ++i) {
    for(int j{}; j < W; ++j) {
      std::cin >> vec[i][j];
    }
  }

  bool found{};
  for(int i{}; i < H; ++i) {
    for(int j{}; j < W; ++j) {
      if(map[i][j]) continue;

      map[i][j] = 1;
      int const m{vec[i][j]};
      using Point = std::pair<int, int>;
      std::stack<Point> stack;
      stack.emplace(i, j);
      int last{};
      int const up{1}, right{2}, down{3}, left{4};
      std::set<Point> chain;
      chain.emplace(i, j);

      while(!stack.empty()) {
        Point const current{stack.top()};
        int const h = current.first;
        int const w = current.second;
//        std::cout << "## " << h << ' ' << w << std::endl;

        if(last != up && h != 0) {
          if(vec[h - 1][w] == m) {
 //           std::cout << h - 1 << ' ' << w << std::endl;
            auto it = chain.find(std::make_pair(h - 1, w));
            if(it != end(chain)) {
              found = true;
              goto exit;
            }
            if(!map[h - 1][w]) {
              chain.emplace(h - 1, w);
              stack.emplace(h - 1, w);
              map[h - 1][w] = 1;
              last = down;
              continue;
            }
          }
        }
        if(last != right && w + 1 != W) {
                                        if(vec[h][w + 1] == m) {
  //          std::cout << h << ' ' << w + 1 << std::endl;
                                                auto it = chain.find(std::make_pair(h, w + 1));
            if(it != end(chain)) {
              found = true;
              goto exit;
            }
            if(!map[h][w + 1]) {
              chain.emplace(h, w + 1);
              stack.emplace(h, w + 1);
              map[h][w + 1] = 1;
              last = left;
              continue;
            }
                                        }
        }
        if(last != down && h + 1 != H) {
          if(vec[h + 1][w] == m) {
   //         std::cout << h + 1 << ' ' << w << std::endl;
                                                auto it = chain.find(std::make_pair(h + 1, w));
            if(it != end(chain)) {
              found = true;
              goto exit;
            }
            if(!map[h + 1][w]) {
              chain.emplace(h + 1, w);
              stack.emplace(h + 1, w);
              map[h + 1][w] = 1;
              last = up;
              continue;
            }
          }
        }
        if(last != left && w != 0) {
                                        if(vec[h][w - 1] == m) {
    //        std::cout << h << ' ' << w - 1 << std::endl;
                                                auto it = chain.find(std::make_pair(h, w - 1));
            if(it != end(chain)) {
              found = true;
              goto exit;
            }
            if(!map[h][w - 1]) {
              chain.emplace(h, w - 1);
              stack.emplace(h, w - 1);
              map[h][w - 1] = 1;
              last = right;
            continue;
            }
                                        }
        }
     //   std::cout << "pop!" << std::endl;
        stack.pop();
        chain.clear();
      }
    }
  }
  exit:

  std::cout << (found ? "possible" : "impossible") << std::endl;

//  for(int i{}; i < H; ++i) {
//    for(int j{}; j < W; ++j) {
//      std::cout << vec[i][j] << ' ';
//    }
//    std::cout << std::endl;
//  }
//
  return 0;
}
0