結果
問題 | No.13 囲みたい! |
ユーザー |
![]() |
提出日時 | 2019-08-30 12:27:53 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4 ms / 5,000 ms |
コード長 | 1,756 bytes |
コンパイル時間 | 2,079 ms |
コンパイル使用メモリ | 190,544 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-20 20:28:18 |
合計ジャッジ時間 | 2,738 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
#include "bits/stdc++.h" #define ALL(obj) (obj).begin(),(obj).end() #define RALL(obj) (obj).rbegin(),(obj).rend() #define REP(i, n) for(int i = 0; i < (int)(n); i++) #define REPR(i, n) for(int i = (int)(n); i >= 0; i--) #define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++) using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int INF = MOD - 1; const ll LLINF = 4e18; vector<vector<int>> m; vector<vector<bool>> visited; int w, h; const int dx[] = {0,-1,0,1}, dy[] = {1,0,-1,0}; bool dfs(int a, int b) { typedef tuple<int, int, int, int> T; stack<T> sta; sta.push({a, b, -1, -1}); while (!sta.empty()) { T p = sta.top(); sta.pop(); int x = get<0>(p), y = get<1>(p), parx = get<2>(p), pary = get<3>(p); visited[x][y] = true; for (int i = 0; i < 4; i++) { if (x + dx[i] >= 0 && x + dx[i] < h && y + dy[i] >= 0 && y + dy[i] < w) { if (x + dx[i] == parx && y + dy[i] == pary) continue; if (m[x + dx[i]][y + dy[i]] == m[x][y]) { if (visited[x + dx[i]][y + dy[i]]) { return true; } else { sta.push({x + dx[i],y + dy[i],x,y}); } } } } } return false; } int main() { cin >> w >> h; m.resize(h, vector<int>(w)); visited.resize(h, vector<bool>(w, false)); REP(i, h) { REP(j, w) { cin >> m[i][j]; } } REP(i, h) { REP(j, w) { if (!visited[i][j] && dfs(i, j)) { puts("possible"); return 0; } } } puts("impossible"); getchar(); getchar(); }