結果

問題 No.13 囲みたい!
ユーザー ttakano
提出日時 2017-12-06 11:43:06
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 1,412 bytes
コンパイル時間 1,291 ms
コンパイル使用メモリ 166,428 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-29 00:57:35
合計ジャッジ時間 1,975 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define print(x) cout<<x<<endl;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a) for(int i=0;i<a;i++)
#define printall(n,array) for(int i=0;i<n;i++){cout<<array[i]<<" ";}cout<<endl;
#define U() cout<<endl;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 1000000007; //10^9+7

int w,h;
int m[102][102];
int cp[102][102];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};


bool check(int y,int x, int _){
  if((y>=0&&y<h&&x>=0&&x<w)&&(cp[y][x]==_||cp[y][x]==0))return true;
  return false;
}


int main(){
  cin>>w>>h;
  REP(i,h)REP(j,w)cin>>m[i][j];
  REP(_,1001){
    REP(i,h)REP(j,w)cp[i][j]=m[i][j];
    REP(i,h)REP(j,w)if(cp[i][j]==_){
      cp[i][j]=0;
      queue<V> que;
      V now=make_pair(-1,make_pair(i,j));
      que.push(now);
      while(!que.empty()){
        V pre=que.front();
        PI pred=pre.second;
        que.pop();
        REP(k,4){
          if(k==pre.first)continue;
          int ny=pred.first+dy[k];
          int nx=pred.second+dx[k];
          if(check(ny,nx,_)==0)continue;
          if(cp[ny][nx]==0){
            print("possible");
            return 0;
          }
          if(cp[ny][nx]==_){
          cp[ny][nx]=0;
          V next=make_pair((k+2)%4,make_pair(ny,nx));
          que.push(next);
          }
        }
      }
    }
  }
  print("impossible");
}
0