結果

問題 No.13 囲みたい!
ユーザー どららどらら
提出日時 2015-06-02 00:28:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,670 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 88,324 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:18:45
合計ジャッジ時間 1,690 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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);
          }
        }
      }
    }
  }

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