結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  ayame_py | 
| 提出日時 | 2016-01-05 03:13:57 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 1,298 bytes | 
| コンパイル時間 | 1,196 ms | 
| コンパイル使用メモリ | 158,060 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-13 10:48:02 | 
| 合計ジャッジ時間 | 1,896 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i,n,m) for (int i=n; i<(int)(m); i++)
#define INF 1000000007
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int W,H;
int M[100][100];
int p[100][100];
bool chk(int y, int x){
    return 0<=y&&y<H&&0<=x&&x<W;
}
bool dfs(int pre_y, int pre_x, int y, int x){
    //訪れたフラグ
    p[y][x]=1;
    REP(i,4){
        int nex_y=y+dy[i];
        int nex_x=x+dx[i];
        //範囲外ではないか、 前回チェックしたものではないか、同じ数字か
        if( chk( nex_y, nex_x ) && ( pre_y!=nex_y||pre_x!=nex_x ) && M[y][x]==M[nex_y][nex_x] ){
            //既に訪れた==閉路
            if(p[nex_y][nex_x]) return true;
            //次を探索してtrueならreturn
            if( dfs(y,x,nex_y,nex_x) ) return true;
        }
    }
    return false;
}
int main(){
    cin >> W >> H;
    REP(i,H){
        REP(j,W){
            cin >> M[i][j];
        }
    }
    
    REP(i,H){
        REP(j,W){
            //もうチェックした
            if(p[i][j]) continue;
            if(dfs(-1,-1,i,j)){
                cout << "possible" << endl;
                return 0;
            }
        }
    }
    
    cout << "impossible" << endl;
    
    return 0;
}
            
            
            
        