結果

問題 No.13 囲みたい!
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-29 08:47:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 427 ms / 5,000 ms
コード長 1,520 bytes
コンパイル時間 1,983 ms
コンパイル使用メモリ 163,708 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:25:58
合計ジャッジ時間 3,047 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <----!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;

const int MAX_H = 100;
const int MAX_W = 100;

int W, H;
int a[MAX_H][MAX_W];
bool done[MAX_H][MAX_W];

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

bool inside(int x, int y) {
    return 0 <= x && x < H && 0 <= y && y < W;
}

// 値valを用いて閉路ができるならtrueを返す
bool dfs(int x, int y, int px, int py, int val) {
    bool ret = false;
    done[x][y] = true;
    rep(k, 4) {
        int nx = x + dx[k], ny = y + dy[k];
        if (nx == px && ny == py) continue;
        if (!inside(nx, ny)) continue;
        if (a[nx][ny] != val) continue;
        if (done[nx][ny]) {
            ret |= true;
            continue;
        }
        ret |= dfs(nx, ny, x, y, val);
    }
    return ret;
}

void init() {
    rep(i, H) rep(j, W) done[i][j] = false;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> W >> H;
    rep(i, H) rep(j, W) cin >> a[i][j];

    rep(i, H) {
        rep(j, W) {
            init();
            if (dfs(i, j, -1, -1, a[i][j])) {
                cout << "possible" << endl;
                return 0;
            }
        }
    }

    cout << "impossible" << endl;

}
0