結果

問題 No.13 囲みたい!
ユーザー ktgktg
提出日時 2016-04-25 22:59:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,728 bytes
コンパイル時間 793 ms
コンパイル使用メモリ 78,576 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:25:55
合計ジャッジ時間 1,328 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0