結果
問題 | No.13 囲みたい! |
ユーザー | ttkkggww |
提出日時 | 2022-03-09 02:53:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,477 bytes |
コンパイル時間 | 4,211 ms |
コンパイル使用メモリ | 257,340 KB |
最終ジャッジ日時 | 2025-01-28 07:50:38 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 15 WA * 1 |
ソースコード
#include<bits/stdc++.h> using namespace std; #include<atcoder/all> using namespace atcoder; using ll = long long; int h,w; vector<vector<int>> G; using P = pair<int,int>; int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1}; void solve(){ stack<P> Q; stack<P> Qp; bool is = false; vector<vector<int>> visited(h,vector<int>(w)); for(int i = 0;i<h;i++){ for(int j = 0;j<w;j++){ if(visited[i][j])continue; int curIdx = i*w+j+1; visited[i][j] = curIdx; Q.emplace(i,j); Qp.emplace(-1,-1); while(Q.size()){ auto [x,y] = Q.top(); auto [px,py] = Qp.top(); //if(px!=-1)cout<<G[x][y]<<' '<<G[px][py]<<endl; //cout<<x<<' '<<y<<endl; Q.pop(); Qp.pop(); for(int k = 0;k<4;k++){ int nx = x+dx[k],ny = y+dy[k]; if(nx<0||nx>=h||ny<0||ny>=w)continue; if(G[x][y]==G[nx][ny]){ if(visited[nx][ny]==curIdx&&nx!=px&&ny!=py){ //cout<<nx<<' '<<ny<<' '<<px<<' '<<py<<endl; is = true; } if(visited[nx][ny]==0){ visited[nx][ny] = curIdx; Q.emplace(nx,ny); Qp.emplace(x,y); } } } } } } /* for(int i = 0;i<h;i++){ for(int j = 0;j<w;j++){ cout<<visited[i][j]<<' '; } cout<<endl; } */ 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(); }