結果
問題 |
No.13 囲みたい!
|
ユーザー |
![]() |
提出日時 | 2022-12-19 11:39:09 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 5 ms / 5,000 ms |
コード長 | 1,165 bytes |
コンパイル時間 | 5,471 ms |
コンパイル使用メモリ | 138,860 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-18 00:49:40 |
合計ジャッジ時間 | 2,333 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
#include <iostream> #include <vector> #include <cmath> #include <map> #include <set> #include <iomanip> #include <queue> using namespace std; int H, W; vector<vector<bool>> visit; vector<vector<int>> field; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void dfs(int h, int w, int n, int ph, int pw){ int nh, nw; visit[h][w] = 1; for (int dir=0; dir < 4; dir++){ nh = h + dx[dir]; nw = w + dy[dir]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (field[nh][nw] != n) continue; if (nh == ph && nw == pw) continue; if (visit[nh][nw]){ cout << "possible" << endl; exit(0); } dfs(nh, nw, n, h, w); } } int main(){ cin >> H >> W; swap(H, W); field.resize(H, vector<int>(W)); visit.resize(H, vector<bool>(W)); for (int i=0; i < H; i++){ for (int j=0; j<W; j++) cin >> field[i][j]; } for (int i=0; i<H; i++){ for (int j=0; j<W; j++){ if (!visit[i][j]){ dfs(i, j, field[i][j], -1, -1); } } } cout << "impossible" << endl; return 0; }