結果

問題 No.13 囲みたい!
ユーザー MisterMister
提出日時 2020-08-19 20:14:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,373 bytes
コンパイル時間 849 ms
コンパイル使用メモリ 95,392 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 09:50:03
合計ジャッジ時間 1,617 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

const std::vector<std::pair<int, int>> dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void solve() {
    int h, w;
    std::cin >> w >> h;

    auto grid = vec(h, vec(w, 0));
    for (auto& v : grid) {
        for (auto& x : v) std::cin >> x;
    }

    auto visited = vec(h, vec(w, 0));

    std::function<void(int, int, int, int)> dfs =
        [&](int x, int y, int px, int py) {
            visited[x][y] = 2;

            for (auto [dx, dy] : dxys) {
                int nx = x + dx, ny = y + dy;
                if (nx < 0 || h <= nx ||
                    ny < 0 || w <= ny ||
                    (nx == px && ny == py) ||
                    grid[nx][ny] != grid[x][y]) continue;

                if (visited[nx][ny] == 2) {
                    std::cout << "possible\n";
                    std::exit(0);
                }
                dfs(nx, ny, x, y);
            }

            visited[x][y] = 1;
        };

    for (int sx = 0; sx < h; ++sx) {
        for (int sy = 0; sy < w; ++sy) {
            if (visited[sx][sy] == 0) dfs(sx, sy, -1, -1);
        }
    }

    std::cout << "impossible\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0