結果

問題 No.13 囲みたい!
ユーザー face4face4
提出日時 2018-09-26 13:56:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,096 bytes
コンパイル時間 499 ms
コンパイル使用メモリ 66,220 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 13:42:14
合計ジャッジ時間 1,150 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
using namespace std;

#define inRange(x,a,b) (a <= x && x < b)

int w, h;
int mat[100][100] = {};
bool visit[100][100] = {};
bool tmpans;

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

bool dfs(int bi, int bj, int i, int j){
    visit[i][j] = true;
    for(int k = 0; k < 4; k++){
        int ni = i + dx[k], nj = j + dy[k];
        if(ni == bi && nj == bj)    continue;
        if(inRange(ni,0,h) && inRange(nj,0,w) && mat[ni][nj] == mat[i][j]){
            if(visit[ni][nj]){
                // 閉路完成
                return true;
            }
            if(dfs(i,j,ni,nj))  return true;
        }
    }
    return false;
}

int main(){
    cin >> w >> h;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> mat[i][j];
        }
    }

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(visit[i][j]) continue;
            if(dfs(-1,-1,i,j)){
                cout << "possible" << endl;
                return 0;
            }
        }
    }

    cout << "impossible" << endl;
    
    return 0;
}
0