結果

問題 No.13 囲みたい!
ユーザー maine_honzuki
提出日時 2020-05-04 16:51:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 921 bytes
コンパイル時間 1,513 ms
コンパイル使用メモリ 168,504 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-24 16:33:42
合計ジャッジ時間 2,358 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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