結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2015-09-04 04:13:23 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 2,039 bytes | 
| コンパイル時間 | 631 ms | 
| コンパイル使用メモリ | 82,508 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-13 10:47:01 | 
| 合計ジャッジ時間 | 1,239 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
#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;
}
            
            
            
        