結果

問題 No.13 囲みたい!
ユーザー drymousedrymouse
提出日時 2024-02-17 11:36:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 5,000 ms
コード長 3,615 bytes
コンパイル時間 854 ms
コンパイル使用メモリ 73,536 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-17 11:36:10
合計ジャッジ時間 2,392 ms
ジャッジサーバーID
(参考情報)
judge13 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
    int W, H;
    cin >> W >> H;
    int M[H + 2][W + 2];
    int maxM = 0;
    for (int i = 0; i < H + 2; i++) {
        for (int j = 0; j < W + 2; j++) {
            if (i*j == 0 || (i-H-1)*(j-W-1) == 0) {
                M[i][j] = 0;
            } else {
                cin >> M[i][j];
                maxM = max(maxM, M[i][j]);
            }
        }
    }

    //-1: なし 0: 未探索 >1: 探索済
    for (int k = 1; k < maxM + 1; k++) {
        int numver = 0; // the number of vertices
        int vertices[H + 2][W + 2];
        for (int i = 0; i < H + 2; i++) {
            for (int j = 0; j < W + 2; j++) {
                if (M[i][j] == k) {
                    numver++;
                    vertices[i][j] = 0;
                } else {
                    vertices[i][j] = -1;
                }
            }
        }

        int step = 1;
        for (int i = 1; i < H + 1; i++) {
            for (int j = 1; j < W + 1; j++) {
                if (vertices[i][j] == 0) {
                    vertices[i][j] = step;
                    for (int l = 0; l < numver; l++) {
                        int changed = 0;
                        for (int i2 = 1; i2 < H + 1; i2++) {
                            for (int j2 = 1; j2 < W + 1; j2++) {
                                if (vertices[i2][j2] == step) {
                                    int counter = 0;
                                    int* tar = &vertices[i2][j2-1];
                                    if (*tar == 0) {
                                        *tar = step + 1;
                                        changed = 1;
                                    } else if (*tar > 0 && *tar <= step) {
                                        counter++;
                                    }
                                    tar = &vertices[i2][j2+1];
                                    if (*tar == 0) {
                                        *tar = step + 1;
                                        changed = 1;
                                    } else if (*tar > 0 && *tar <= step) {
                                        counter++;
                                    }
                                    tar = &vertices[i2-1][j2];
                                    if (*tar == 0) {
                                        *tar = step + 1;
                                        changed = 1;
                                    } else if (*tar > 0 && *tar <= step) {
                                        counter++;
                                    }
                                    tar = &vertices[i2+1][j2];
                                    if (*tar == 0) {
                                        *tar = step + 1;
                                        changed = 1;
                                    } else if (*tar > 0 && *tar <= step) {
                                        counter++;
                                    }
                                    if (counter > 1) {
                                        cout << "possible" << endl;
                                        return 0;
                                    }
                                }
                            }
                        }
                        step++;
                        if (!changed) {
                            break;
                        }
                    }
                } else {
                    
                }
            }
        }
    }
     cout << "impossible" << endl;
    return 0;
}
0