結果

問題 No.13 囲みたい!
ユーザー A63635985A63635985
提出日時 2018-03-01 22:54:55
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,728 bytes
コンパイル時間 989 ms
コンパイル使用メモリ 72,376 KB
実行使用メモリ 814,648 KB
最終ジャッジ日時 2023-08-26 22:02:50
合計ジャッジ時間 8,594 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 TLE -
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <map>
#include <vector>
using namespace std;
struct Struct{
        int h,w,count;
        bool operator<(const Struct &other)const{
                if(h==other.h)
                        return w<other.w;
                else
                        return h<other.h;
        }
};
int H,W;
int direction[5]={-1,0,1,0,-1};
int field[101][101];
Struct start;
bool recursion(Struct now,int color,map<Struct,bool>past){
        if(now.h<0 || now.h>=H)return false;
        if(now.w<0 || now.w>=W)return false;
        if(field[now.h][now.w]!=color)return false;
        if(now.h==start.h && now.w==start.w && now.count>3)return true;
        if(past[now])return false;
        past[now]=true;
        bool response=false;
        for(int i=0 ; i<4 ; i++ ){
                Struct next;
                next.h=now.h+direction[i];
                next.w=now.w+direction[i+1];
                next.count=now.count+1;
                response|=recursion(next,color,past);
        }
        return response;
}
int main(){
        cin >> W >> H;
        for(int i=0 ; i<H ; i++ )
                for(int j=0 ; j<W ; j++ )
                        cin >> field[i][j];
        for(int i=0 ; i<H ; i++ ){
                for(int j=0 ; j<W ; j++ ){
                        map<Struct,bool> past;
                        start.h=i;start.w=j;
                        Struct now;
                        now.h=i;now.w=j;now.count=0;
                        if(recursion(now,field[i][j],past)){
                                cout<<"possible"<<endl;
                                return 0;
                        }
                }
        }
        cout<<"impossible"<<endl;
        return 0;
}
0