結果

問題 No.13 囲みたい!
ユーザー koba-e964koba-e964
提出日時 2015-06-19 13:16:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,470 bytes
コンパイル時間 881 ms
コンパイル使用メモリ 86,636 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:19:01
合計ジャッジ時間 1,445 ms
ジャッジサーバーID
(参考情報)
judge5 / 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 5 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PI;
const double EPS=1e-9;

int w, h;
const int W = 101;
int m[W][W];

int dp[W][W] = {};

int col(int x, int y) {
  if (x < 0 || x >= h || y < 0 || y >= w) {
    return -1;
  }
  return m[x][y];
}

int dfs(int x, int y, int bx, int by) {
  if (bx == -1 && by == -1 && dp[x][y]) {
    return 0;
  }
  if (dp[x][y]) {
    return 1;
  }
  dp[x][y] = 1;
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  REP(i, 0, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (col(nx, ny) != m[x][y]) {
      continue;
    }
    if (nx != bx || ny != by) {
      int sub = dfs(nx, ny, x, y);
      if (sub) {
	return sub;
      }
    }
  }
  return 0;
}

int main(void){
  cin >> w >> h;
  REP(i, 0, h) {
    REP(j, 0, w) {
      cin >> m[i][j];
    }
  }
  int res = 0;
  REP(i, 0, h) {
    REP(j, 0, w) {
      res |= dfs(i, j, -1, -1);
    }
  }
  cout << (res ? "possible" : "impossible") << endl;
}
0