結果

問題 No.13 囲みたい!
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-08-30 12:27:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,756 bytes
コンパイル時間 2,193 ms
コンパイル使用メモリ 189,424 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-30 22:15:13
合計ジャッジ時間 2,932 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = (int)(n); i >= 0; i--)
#define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = MOD - 1;
const ll LLINF = 4e18;

vector<vector<int>> m;
vector<vector<bool>> visited;
int w, h;

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

bool dfs(int a, int b) {
    typedef tuple<int, int, int, int> T;
    stack<T> sta;
    sta.push({a, b, -1, -1});
    while (!sta.empty()) {
        T p = sta.top(); sta.pop();
        int x = get<0>(p), y = get<1>(p), parx = get<2>(p), pary = get<3>(p);
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            if (x + dx[i] >= 0 && x + dx[i] < h && y + dy[i] >= 0 && y + dy[i] < w) {
                if (x + dx[i] == parx && y + dy[i] == pary) continue;
                if (m[x + dx[i]][y + dy[i]] == m[x][y]) {
                    if (visited[x + dx[i]][y + dy[i]]) {
                        return true;
                    }
                    else {
                        sta.push({x + dx[i],y + dy[i],x,y});
                    }
                }
            }
        }
    }
    return false;
}

int main() {
    cin >> w >> h;
    m.resize(h, vector<int>(w));
    visited.resize(h, vector<bool>(w, false));
    REP(i, h) {
        REP(j, w) {
            cin >> m[i][j];
        }
    }
    REP(i, h) {
        REP(j, w) {
            if (!visited[i][j] && dfs(i, j)) {
                puts("possible");
                return 0;
            }
        }
    }
    puts("impossible");
    getchar(); getchar();
}
0