結果

問題 No.13 囲みたい!
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-19 11:39:09
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,165 bytes
コンパイル時間 3,433 ms
コンパイル使用メモリ 102,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 09:39:37
合計ジャッジ時間 5,665 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>

using namespace std;

int H, W;

vector<vector<bool>> visit;
vector<vector<int>> field;

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

void dfs(int h, int w, int n, int ph, int pw){
    int nh, nw;
    visit[h][w] = 1;

    for (int dir=0; dir < 4; dir++){
        nh = h + dx[dir];
        nw = w + dy[dir];

        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (field[nh][nw] != n) continue;
        if (nh == ph && nw == pw) continue;
        if (visit[nh][nw]){
            cout << "possible" << endl;
            exit(0);
        }

        dfs(nh, nw, n, h, w);
    }
}

int main(){

    cin >> H >> W;
    swap(H, W);
    field.resize(H, vector<int>(W));
    visit.resize(H, vector<bool>(W));

    for (int i=0; i < H; i++){
        for (int j=0; j<W; j++) cin >> field[i][j];
    }

    for (int i=0; i<H; i++){
        for (int j=0; j<W; j++){
            if (!visit[i][j]){
                dfs(i, j, field[i][j], -1, -1);
            }
        }
    }

    cout << "impossible" << endl;

    return 0;
}
0