結果

問題 No.13 囲みたい!
ユーザー mizunomidorimizunomidori
提出日時 2016-07-01 23:46:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,515 bytes
コンパイル時間 874 ms
コンパイル使用メモリ 88,872 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:27:00
合計ジャッジ時間 1,565 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>

#define W_MAX 100
#define H_MAX 100

using namespace std;

class undirected_graph
{
    private:
        const int number_of_nodes;
        vector<int> *edges;
    public:
        undirected_graph(int n) : number_of_nodes(n) {edges = new vector<int>[number_of_nodes];}
        int size() {return number_of_nodes;}
        void make_edge(int x, int y)
        {
            edges[x].push_back(y);
            edges[y].push_back(x);
        }
        bool has_loop()
        {
            bool *used = new bool[number_of_nodes]();
            for (int i = 0; i < number_of_nodes; i++) {
                if (!used[i]) {
                    queue<pair<int, int> > q;
                    q.push(pair<int, int>(i, -1));
                    while (!q.empty()) {
                        int x, previous;
                        tie(x, previous) = q.front();
                        q.pop();
                        used[x] = true;
                        for (int j = 0; j < edges[x].size(); j++) {
                            int to = edges[x][j];
                            if (to == previous) {
                                continue;
                            } else if (used[to]) {
                                return true;
                            } else {
                                q.push(pair<int, int>(to, x));
                            }
                        }
                    }
                }
            }
            return false;
        }
};

int main(void)
{
    int W, H, M[W_MAX][H_MAX];
    cin >> W >> H;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> M[j][i];
        }
    }
    undirected_graph g(W * H);
    for (int i = 0; i < W - 1; i++) {
        for (int j = 0; j < H; j++) {
            if (M[i][j] == M[i + 1][j]) {
                g.make_edge(i*H + j, (i+1)*H + j);
            }
        }
    }
    for (int i = 0; i < W; i++) {
        for (int j = 0; j < H - 1; j++) {
            if (M[i][j] == M[i][j + 1]) {
                g.make_edge(i*H + j, i*H + (j+1));
            }
        }
    }
    if (g.has_loop()) {
        cout << "possible" << endl;
    } else {
        cout << "impossible" << endl;
    }
    return 0;
}
0