結果

問題 No.13 囲みたい!
ユーザー たこしたこし
提出日時 2015-06-08 18:05:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,583 bytes
コンパイル時間 737 ms
コンパイル使用メモリ 90,044 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:18:46
合計ジャッジ時間 1,330 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define INF_MIN 100000000
#define INF 1145141919
#define INF_MAX 2147483647
#define LL_MAX 9223372036854775807
#define EPS 1e-10
#define PI acos(-1)
#define LL long long

using namespace std;

#define MAX_W 101
#define MAX_H 101

int W, H;

int maze[MAX_H][MAX_W];
int used[MAX_H][MAX_W];

int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

bool solve(int x, int y, int pre){

  if(used[y][x] == maze[y][x])
    return true;

  used[y][x] = maze[y][x];
  
  for(int i = 0; i < 4; i++){
    
    if(i == pre)
      continue;

    int nextpre = (i + 2) % 4;

    int nx = x + dx[i];
    int ny = y + dy[i];

    if(0 <= nx && nx < W && 0 <= ny && ny < H && maze[ny][nx] == maze[y][x]){
      if(solve(nx, ny, nextpre))
	return true;
    }
    
  }

  return false;

}

int main(){

  cin >> W >> H;
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      cin >> maze[i][j];
    }
  }

  memset(used, 0, sizeof(used));

  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      if(!used[i][j]){
	if(solve(j, i, -1)){
	  cout << "possible" << endl;
	  return 0;
	}
      }
    }
  }

  cout << "impossible" << endl;

  return 0;

}
0