結果

問題 No.13 囲みたい!
ユーザー face4face4
提出日時 2018-09-26 13:40:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 5,000 ms
コード長 1,305 bytes
コンパイル時間 546 ms
コンパイル使用メモリ 65,136 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 12:11:11
合計ジャッジ時間 1,614 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 96 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 202 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 12 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 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 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 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};

void 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]){
                // 閉路完成
                tmpans = true;
                return;
            }
            dfs(i,j,ni,nj);
        }
    }
    visit[i][j] = false;
}

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

    bool judge = false;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            tmpans = false;
            if(i != h-1 && mat[i][j] == mat[i+1][j]){
                dfs(i,j,i+1,j);
            }
            if(tmpans)  judge = true;

            if(j != w-1 && mat[i][j] == mat[i][j+1]){
                dfs(i,j,i,j+1);
            }
        }
    }

    if(judge)   cout << "possible" << endl;
    else        cout << "impossible" << endl;
    
    return 0;
}
0