結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2014-10-31 13:07:50 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 5 ms / 5,000 ms | 
| コード長 | 1,413 bytes | 
| コンパイル時間 | 1,108 ms | 
| コンパイル使用メモリ | 67,096 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-13 10:38:10 | 
| 合計ジャッジ時間 | 1,416 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct State {
    int y, x, py, px;
};
int W, H;
int M[111][111];
bool used[111][111];
bool bfs(int y, int x){
    int num = M[y][x];
    
    queue<State> Q;
    Q.push({y, x, -1, -1});
    used[y][x] = true;
    while(!Q.empty()){
        auto ps = Q.front(); Q.pop();
        
        int y = ps.y;
        int x = ps.x;
        int py = ps.py;
        int px = ps.px;
        
        for(int k=0;k<4;k++){
            int ny = y + dy[k];
            int nx = x + dx[k];
            
            if(ny == py && nx == px)continue;
            if(ny < 0 || ny >= H || nx < 0 || nx >= W)continue;
            if(M[ny][nx] != num)continue;
            
            if(used[ny][nx])return true;
            used[ny][nx] = true;
            Q.push({ny, nx, y, x});
        }
    }
    
    return false;
}
int main(){
    cin >> W >> H;
    for(int y=0;y<H;y++)for(int x=0;x<W;x++)cin >> M[y][x];
    
    for(int y=0;y<H;y++)for(int x=0;x<W;x++){
        if(used[y][x])continue;
        
        bool res = bfs(y, x);
        if(res){
            cout << "possible" << endl;
            return 0;
        }
    }
    
    cout << "impossible" << endl;
    
    return 0;
}
            
            
            
        