結果

問題 No.13 囲みたい!
ユーザー ctyl_0ctyl_0
提出日時 2015-09-04 04:13:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,039 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 81,992 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:21:14
合計ジャッジ時間 1,359 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;

using namespace std;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

void inputArray(int * p, int a){
    rep(i, a){
        cin >> p[i];
    }
};

void inputVector(vector<int> * p, int a){
    rep(i, a){
        int input;
        cin >> input;
        p -> push_back(input);
    }
}

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

int M[100][100];
bool V[100][100] = {false};
int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};
int inv[4] = {1, 0, 3, 2};

bool dfs(int x, int y, int dir, int w, int h){
    bool ret = false;
    V[x][y] = true;
    rep(i, 4){
        if (inv[i] != dir && x + vx[i] >= 0 && x + vx[i] < w && y + vy[i] >= 0 && y + vy[i] < h) {
            if ( M[x][y] == M[x + vx[i]][y + vy[i]] ) {
                if ( V[x + vx[i]][y + vy[i]] == true ) {
                    ret = true;
                    break;
                }
                else{
                    ret = dfs(x + vx[i], y + vy[i], i, w, h);
                }
            }
        }
    }
    return ret;
}

int main(int argc, const char * argv[]) {
    
    // source code
    int W = inputValue();
    int H = inputValue();
    
    rep(i, H){
        rep(j, W){
            M[j][i] = inputValue();
        }
    }
    
    bool ret = false;
    
    rep(i, H){
        
        rep(j, W){
            if ( V[j][i] == false) {
                ret = dfs(j, i, 5, W, H);
                if (ret == true) {
                    break;
                }
            }
        }
        if (ret == true) {
            break;
        }
    }
    
    output((ret == true) ? "possible" : "impossible", 0);
    
    return 0;
}
0