結果

問題 No.13 囲みたい!
ユーザー simansiman
提出日時 2016-03-29 06:20:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 661 ms / 5,000 ms
コード長 1,657 bytes
コンパイル時間 677 ms
コンパイル使用メモリ 73,508 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:24:10
合計ジャッジ時間 2,161 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 661 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 28 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 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 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <limits.h>
#include <time.h>
#include <string>
#include <string.h>
#include <sstream>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;

typedef long long ll;

const int DY[4] = {-1, 0, 0, 1};
const int DX[4] = { 0,-1, 1, 0};

const int MAX_H = 100;
const int MAX_W = 100;

struct Coord {
  int y;
  int x;
  int dist;

  Coord(int y = -1, int x = -1, int dist = -1){
    this->y = y;
    this->x = x;
    this->dist = dist;
  }
};

int w, h;
int field[MAX_H][MAX_W];
int oy, ox;
int checkCount;
bool success;
int checkField[MAX_H][MAX_W];

void dfs(int y, int x, int dist, int num){
  if(success) return;
  if(field[y][x] != num) return;

  if(y == oy && x == ox && dist >= 4){
    success = true;
    return;
  }
  if(checkField[y][x] == checkCount) return;
  checkField[y][x] = checkCount;

  for(int i = 0; i < 4; i++){
    int ny = y + DY[i];
    int nx = x + DX[i];

    if(ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
    dfs(ny, nx, dist+1, num);
  }
}

int main(){
  cin >> w >> h;

  memset(field, -1, sizeof(field));
  memset(checkField, -1, sizeof(checkField));
  success = false;
  checkCount = 0;

  for(int y = 0; y < h; y++){
    for(int x = 0; x < w; x++){
      cin >> field[y][x];
    }
  }

  for(int y = 0; y < h; y++){
    for(int x = 0; x < w; x++){
      int num = field[y][x];
      oy = y;
      ox = x;

      checkCount++;
      dfs(y, x, 0, num);
    }
  }

  if(success){
    cout << "possible" << endl;
  }else{
    cout << "impossible" << endl;
  }
  return 0;
}
0