結果

問題 No.13 囲みたい!
ユーザー どららどらら
提出日時 2015-06-02 00:19:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,707 bytes
コンパイル時間 674 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 814,608 KB
最終ジャッジ日時 2023-09-20 18:25:04
合計ジャッジ時間 4,849 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

int W, H;
int field[105][105];
vector<int> G[10005];

int vx[4] = {0,0,1,-1};
int vy[4] = {1,-1,0,0};
int xx[1005];
int visited[10005];

bool check(int s){
  queue< pair<int,int> > que;
  que.push(make_pair(s, -1));
  while(!que.empty()){
    pair<int,int> p = que.front(); que.pop();
    visited[p.first] = 1;
    rep(i,G[p.first].size()){
      if(G[p.first][i] == p.second) continue;
      if(visited[G[p.first][i]] == 1) return true;
      que.push(make_pair(G[p.first][i], p.first));
    }
  }
  return false;
}

int main(){
  cin >> W >> H;

  rep(i,H){
    rep(j,W){
      cin >> field[i][j];
    }
  }

  rep(i,1005){
    xx[i] = -1;
  }
  rep(i,10005) visited[i] = 0;

  rep(i,H){
    rep(j,W){
      xx[field[i][j]] = i*W+j;
      rep(k,4){
        int nx = j+vx[k], ny = i+vy[k];
        if(0<=nx && nx<W && 0<=ny && ny<H){
          if(field[i][j] == field[ny][nx]){
            G[i*W+j].push_back(ny*W+nx);
            G[ny*W+nx].push_back(i*W+j);
          }
        }
      }
    }
  }

  rep(i,H){
    rep(j,W){
      if(visited[0] == 0){
        if(check(i*W+j)){
          cout << "possible" << endl;
          return 0;
        }
      }
    }
  }
  cout << "impossible" << endl;
  return 0;
}
0