結果

問題 No.13 囲みたい!
ユーザー A63635985A63635985
提出日時 2018-03-01 23:17:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,067 bytes
コンパイル時間 528 ms
コンパイル使用メモリ 63,960 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 22:11:36
合計ジャッジ時間 1,330 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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];
int dp[101][101];
Struct start;
bool signal;
bool recursion(Struct now,int color){
        if(signal)return true;
        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 signal=true;
        }
        if(dp[now.h][now.w]!=-1){
                if((now.count-dp[now.h][now.w])>3){
                        return signal=true;
                }
                return false;
        }
        dp[now.h][now.w]=now.count;
        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);
        }
        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];
                        dp[i][j]=-1;
                }
        }
        for(int i=0 ; i<H ; i++ ){
                for(int j=0 ; j<W ; j++ ){
                        if(dp[i][j]!=-1)continue;
                        start.h=i;start.w=j;
                        Struct now;
                        now.h=i;now.w=j;now.count=0;
                        signal=false;
                        if(recursion(now,field[i][j])){
                                cout<<"possible"<<endl;
                                return 0;
                        }
                }
        }
        cout<<"impossible"<<endl;
        return 0;
}
0