結果

問題 No.13 囲みたい!
ユーザー ikdikd
提出日時 2016-08-04 14:36:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,034 bytes
コンパイル時間 478 ms
コンパイル使用メモリ 60,380 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 15:18:21
合計ジャッジ時間 961 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 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 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 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<iostream>
#include<set>

using namespace std;
typedef pair<int, int> pii;
int W, H;
int M[100][100];
bool reached[100][100];
bool flag=false;
const pii dd[]={{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool is(int h, int w){
    return (0<=h&&h<H&&0<=w&&w<W);
}

void dfs(int h, int w, int ph, int pw){
    if(flag) return;
    reached[h][w]=true;
    for(auto d: dd){
        auto nh=h+d.first;
        auto nw=w+d.second;
        if(!is(nh, nw)) continue;
        if(nh==ph&&nw==pw) continue;
        if(M[h][w]!=M[nh][nw]) continue;
        if(reached[nh][nw]){
            flag=true;
            return;
        }
        dfs(nh, nw, h, w);
    }
    return;
}

int main(){

    cin>> W>> H;
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            cin>> M[i][j];
        }
    }

    set<int> st;
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            if(reached[i][j]) continue;
            dfs(i, j, -1, -1);
        }
    }

    cout<< (flag? "possible": "impossible")<< endl;
    return 0;
}
0