結果

問題 No.13 囲みたい!
ユーザー GOTKAKO
提出日時 2024-08-04 15:17:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,129 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 207,872 KB
最終ジャッジ日時 2025-02-23 20:59:15
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int H,W; cin >> H >> W; swap(H,W);
    vector<vector<int>> A(H,vector<int>(W));
    for(auto &h : A) for(auto &w : h) cin >> w;

    vector<vector<bool>> visited(H,vector<bool>(W)),already = visited;
    vector<pair<int,int>> dxy = {{-1,0},{0,1},{1,0},{0,-1}};
    bool yes = false;
    auto dfs = [&](auto dfs,int x,int y,int bx = -1,int by = -1){
        visited.at(x).at(y) = true;
        for(auto [dx,dy] : dxy){
            int nx = x+dx,ny = y+dy;
            if(nx < 0 || ny < 0 || nx >= H || ny >= W) continue;
            if(already.at(nx).at(ny) || (nx == bx && ny == by) || A.at(x).at(y) != A.at(nx).at(ny)) continue;
            if(visited.at(nx).at(ny)){yes = true; return;}
            dfs(dfs,nx,ny,x,y);
            if(yes) return;
        }
        already.at(x).at(y) = true;
    };
    
    for(int i=0; i<H; i++) for(int k=0; k<W; k++) if(already.at(i).at(k) == false){
        dfs(dfs,i,k);
        if(yes) break;
    }

    if(!yes) cout << "im"; 
    cout << "possible" << endl;
}
0