結果
| 問題 |
No.13 囲みたい!
|
| ユーザー |
A63635985
|
| 提出日時 | 2018-03-01 23:17:14 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 5,000 ms |
| コード長 | 2,067 bytes |
| コンパイル時間 | 685 ms |
| コンパイル使用メモリ | 64,396 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-26 01:15:25 |
| 合計ジャッジ時間 | 1,408 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
#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;
}
A63635985