結果

問題 No.13 囲みたい!
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-04 16:51:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 921 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 165,584 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-06 22:22:06
合計ジャッジ時間 2,376 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

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

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

void dfs(int x, int y, int pre_x, int pre_y, int n) {
    if (seen[x][y] == n) {
        cout << "possible" << endl;
        exit(0);
    }
    if (seen[x][y])
        return;
    seen[x][y] = n;
    for (int i = 0; i < 4; i++) {
        int nxt_x = x + dx[i];
        int nxt_y = y + dy[i];
        if (nxt_x != pre_x || nxt_y != pre_y) {
            if (M[x][y] == M[nxt_x][nxt_y]) {
                dfs(nxt_x, nxt_y, x, y, n);
            }
        }
    }
}

int main() {
    cin >> W >> H;
    for (int h = 0; h < H; h++)
        for (int w = 0; w < W; w++)
            cin >> M[w + 1][h + 1];
    for (int h = 0; h < H; h++)
        for (int w = 0; w < W; w++)
            dfs(w + 1, h + 1, -1, -1, h * 200 + w + 100);
    cout << "impossible" << endl;
}
0