結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-07-15 05:18:34 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 7 ms / 5,000 ms |
| コード長 | 3,004 bytes |
| コンパイル時間 | 1,543 ms |
| コンパイル使用メモリ | 142,148 KB |
| 最終ジャッジ日時 | 2025-01-11 20:53:31 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:65:20: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
65 | int H, W; scanf("%d %d", &W, &H);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:66:108: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
66 | vector<vector<int>> a(H, vector<int>(W)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) scanf("%d", &a[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
void dfs_tree(int cur, int pre, vector<bool>& used, vector<pair<int, int>>& rest, vector<vector<int>>& g, vector<vector<int>>& tree) {
used[cur] = true;
for (auto next : g[cur]) {
if (next == pre) continue;
if (used[next]) {
rest.emplace_back(cur, next);
}
else {
tree[cur].emplace_back(next);
tree[next].emplace_back(cur);
dfs_tree(next, cur, used, rest, g, tree);
}
}
}
int dh[] = { 1,-1,0,0 };
int dw[] = { 0,0,1,-1 };
int main()
{
/*
cin.tie(nullptr);
ios::sync_with_stdio(false);
*/
int H, W; scanf("%d %d", &W, &H);
vector<vector<int>> a(H, vector<int>(W)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) scanf("%d", &a[i][j]);
vector<vector<bool>> used(H, vector<bool>(W));
vector<bool> f(H * W);
vector<vector<int>> g(H * W);
vector<vector<int>> tree(H * W);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (used[i][j]) continue;
queue<pair<int, int>> q;
used[i][j] = true;
q.emplace(i, j);
while (!q.empty()) {
auto [h, w] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nh = h + dh[i];
int nw = w + dw[i];
if (nh < 0 or nh >= H or nw < 0 or nw >= W) continue;
if (a[nh][nw] != a[h][w]) continue;
if (h < nh or w < nw) g[h * W + w].emplace_back(nh * W + nw), g[nh * W + nw].emplace_back(h * W + w);
if (used[nh][nw]) continue;
used[nh][nw] = true;
q.emplace(nh, nw);
}
}
vector<pair<int, int>> rest;
dfs_tree(i * W + j, -1, f, rest, g, tree);
if (!rest.empty()) {
puts("possible");
return 0;
}
}
}
puts("impossible");
return 0;
}