結果

問題 No.13 囲みたい!
ユーザー kroton
提出日時 2014-10-31 12:54:13
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 985 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 57,176 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-12-30 15:19:01
合計ジャッジ時間 8,332 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int W, H;
int M[111][111];

bool used[111][111];
bool dfs(int y, int x, int py, int px){
    if(y < 0 || y >= H || x < 0 || x >= W)return false;
    if(M[y][x] != M[py][px])return false;
    if(used[y][x])return true;
    
    used[y][x] = true;
    
    bool res = false;
    for(int k=0;k<4;k++){
        int ny = y + dy[k];
        int nx = x + dx[k];
        if(ny == py && nx == px)continue;
        
        res |= dfs(ny, nx, y, x);
    }
    
    used[y][x] = false;
    return res;
}

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++){
        bool res = dfs(y, x, y, x);
        if(res){
            cout << "possible" << endl;
            return 0;
        }
    }
    
    cout << "impossible" << endl;
    
    return 0;
}
0