結果
問題 |
No.13 囲みたい!
|
ユーザー |
![]() |
提出日時 | 2016-08-04 14:36:09 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 4 ms / 5,000 ms |
コード長 | 1,034 bytes |
コンパイル時間 | 563 ms |
コンパイル使用メモリ | 60,504 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-06 23:50:08 |
合計ジャッジ時間 | 1,158 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
#include<iostream> #include<set> using namespace std; typedef pair<int, int> pii; int W, H; int M[100][100]; bool reached[100][100]; bool flag=false; const pii dd[]={{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool is(int h, int w){ return (0<=h&&h<H&&0<=w&&w<W); } void dfs(int h, int w, int ph, int pw){ if(flag) return; reached[h][w]=true; for(auto d: dd){ auto nh=h+d.first; auto nw=w+d.second; if(!is(nh, nw)) continue; if(nh==ph&&nw==pw) continue; if(M[h][w]!=M[nh][nw]) continue; if(reached[nh][nw]){ flag=true; return; } dfs(nh, nw, h, w); } return; } int main(){ cin>> W>> H; for(int i=0; i<H; i++){ for(int j=0; j<W; j++){ cin>> M[i][j]; } } set<int> st; for(int i=0; i<H; i++){ for(int j=0; j<W; j++){ if(reached[i][j]) continue; dfs(i, j, -1, -1); } } cout<< (flag? "possible": "impossible")<< endl; return 0; }