結果

問題 No.13 囲みたい!
ユーザー ayame_pyayame_py
提出日時 2016-01-05 03:13:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,298 bytes
コンパイル時間 1,156 ms
コンパイル使用メモリ 158,056 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:22:08
合計ジャッジ時間 1,655 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0