結果
問題 | No.13 囲みたい! |
ユーザー |
|
提出日時 | 2022-03-09 03:24:08 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4 ms / 5,000 ms |
コード長 | 1,073 bytes |
コンパイル時間 | 4,758 ms |
コンパイル使用メモリ | 254,416 KB |
最終ジャッジ日時 | 2025-01-28 07:51:15 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
#include<bits/stdc++.h> using namespace std; #include<atcoder/all> using namespace atcoder; using ll = long long; int h,w; vector<vector<int>> G; vector<vector<int>> visited; using P = pair<int,int>; bool is = false; int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1}; void dfs(int x,int y,int px,int py,int color){ visited[x][y] = color; for(int i = 0;i<4;i++){ int nx = x+dx[i],ny = y+dy[i]; if(nx<0||nx>=h||ny<0||ny>=w)continue; if(nx==px&&ny==py)continue; if(G[x][y]==G[nx][ny]){ if(visited[nx][ny]==0){ dfs(nx,ny,x,y,color); }else{ is = true; } } } } void solve(){ stack<P> Q; stack<P> Qp; is = false; visited = vector<vector<int>>(h,vector<int>(w)); for(int i = 0;i<h;i++){ for(int j = 0;j<w;j++){ if(visited[i][j]==0){ dfs(i,j,-1,-1,-1); } } } if(is)cout<<"possible"<<endl; else cout<<"impossible"<<endl; } signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin >> w >> h; G = vector<vector<int>>(h,vector<int>(w)); for(int i = 0;i<h;i++){ for(int j = 0;j<w;j++){ cin >> G[i][j]; } } solve(); }