結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  ktg | 
| 提出日時 | 2016-04-25 22:59:47 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 5 ms / 5,000 ms | 
| コード長 | 1,728 bytes | 
| コンパイル時間 | 726 ms | 
| コンパイル使用メモリ | 78,784 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-13 10:52:53 | 
| 合計ジャッジ時間 | 1,373 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <iterator>
//#define pb push_back
//#define puts(x) cout << #x << " : " << x << endl;
//#pragma GCC diagnostic ignored "-Wconversion"
//#define REP(i,n) for (int i=0;i<(n);i++)
//#define REPE(i,n) for (int i=0;i<=(n);i++)
//#define init(a,b) memset((a), (b), (sizeof(a)));
//#define PI 3.14159265
//#define EPS (1e-10)
//#define EQ(a,b) (abs((a)-(b)) < EPS)
using namespace std;
typedef long long ll;
//#define int long long
int dxy[] = {0, 1, 0, -1, 0};
typedef pair<int, int> P;
int W, H;
int mp[200][200];
bool vis[200][200];
bool solve2(int num, int prevh, int prevw, int h, int w){
    for (int i=0;i<4;i++){
        int nh = h + dxy[i];
        int nw = w + dxy[i+1];
        if(nh == prevh && nw == prevw)continue;
        if(0<=nh&&nh<H&&0<=nw&&nw<W&&num==mp[nh][nw]){
            if(vis[nh][nw])return true;
            vis[nh][nw]=1;
            if(solve2(num, h, w, nh, nw)){
                return true;
            }
        }
    }
    return false;
}
bool solve(){
    for(int h=0;h<H;h++){
        for(int w=0;w<W;w++){
            if(!vis[h][w]){
                vis[h][w]=1;
                if (solve2(mp[h][w], -1, -1, h, w)){
                    return true;
                }
            }
        }
    }
    return false;
}
signed main() {
    cin>>W>>H;
    for(int h=0;h<H;h++){
        for(int w=0;w<W;w++){
            cin>>mp[h][w];
        }
    }
    if(solve()){
        cout<<"possible"<<endl;
    }else{
        cout<<"impossible"<<endl;
    }
    return 0;
}
            
            
            
        