結果

問題 No.13 囲みたい!
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-19 05:49:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,005 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 54,364 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 16:44:37
合計ジャッジ時間 1,065 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

const int kMAX_W = 110;
const int kMAX_H = 110;

int M[kMAX_H][kMAX_H];
bool used[kMAX_H][kMAX_W];

int dh[4] = {1, 0, -1, 0},
    dw[4] = {0, 1, 0, -1};

int W, H;

bool DFS(int h, int w, int pre_h, int pre_w) {
    used[h][w] = true;

    for (int i = 0; i < 4; i++) {
        int nh = h + dh[i], nw = w + dw[i];
        if ((nh == pre_h && nw == pre_w) || nh < 0 || nh >= H ||
                nw < 0 || nw >= W || M[nh][nw] != M[h][w]) continue;

        if (used[nh][nw] || DFS(nh, nw, h, w)) return true;
    }
    return false;
}

int main() {
    cin >> W >> H;

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

    for (int h = 0; h < H; h++) {
        for (int w = 0; w < W; w++) {
            if (!used[h][w] && DFS(h, w, -1, -1)) {
                cout << "possible" << endl;
                return 0;
            }
        }
    }

    cout << "impossible" << endl;

    return 0;
}
0