結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-19 23:01:26 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 14 ms / 5,000 ms |
| コード長 | 1,840 bytes |
| コンパイル時間 | 1,193 ms |
| コンパイル使用メモリ | 98,216 KB |
| 最終ジャッジ日時 | 2025-02-10 19:08:59 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
#include<iostream>
#include<vector>
#include<unordered_set>
#include<stack>
bool solve(int W, int H, std::vector<std::vector<int>> M) {
std::vector<std::vector<int>> G(H * W);
auto grid2int = [&W](int h, int w) { return h * W + w; };
for (int h = 0; h < H - 1; h++) {
for (int w = 0; w < W; w++) {
if (M.at(h).at(w) == M.at(h + 1).at(w)) {
G.at(grid2int(h, w)).push_back(grid2int(h + 1, w));
G.at(grid2int(h + 1, w)).push_back(grid2int(h, w));
}
}
}
for (int h = 0; h < H; h++) {
for (int w = 0; w < W - 1; w++) {
if (M.at(h).at(w) == M.at(h).at(w + 1)) {
G.at(grid2int(h, w)).push_back(grid2int(h, w + 1));
G.at(grid2int(h, w + 1)).push_back(grid2int(h, w));
}
}
}
std::unordered_set<int> nodes;
for (int i = 0; i < H * W; i++) { nodes.insert(i); }
while (!nodes.empty()) {
auto v0 = *nodes.begin();
nodes.erase(nodes.begin());
std::stack<std::pair<int, int>> stk;
stk.emplace(v0, -1);
while (!stk.empty()) {
auto [v, p] = stk.top();
stk.pop();
nodes.erase(v);
for (const auto &x: G.at(v)) {
if (x == p) { continue; }
if (nodes.find(x) == nodes.end()) {
return true;
}
stk.emplace(x, v);
}
}
}
return false;
}
int main() {
int W, H;
std::cin >> W >> H;
std::vector<std::vector<int>> M(H, std::vector<int>(W));
for (int h = 0; h < H; h++) {
for (int w = 0; w < W; w++) {
std::cin >> M.at(h).at(w);
}
}
const std::string ans = solve(W, H, M) ? "possible" : "impossible";
std::cout << ans << std::endl;
}