結果

問題 No.13 囲みたい!
ユーザー lovablepleiadlovablepleiad
提出日時 2015-12-11 13:42:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,574 bytes
コンパイル時間 809 ms
コンパイル使用メモリ 92,892 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:22:04
合計ジャッジ時間 1,367 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 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 #

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <stack>
#include <array>
#include <fstream>
#include <deque>
#include <bitset>


#define REP(i,n) for(int (i) = 0;(i) < (n) ; ++(i))
#define REPS(a,i,n) for(int (i) = (a) ; (i) < (n) ; ++(i))
#if defined(_MSC_VER)||__cplusplus > 199711L
#define AUTO(r,v) auto r = (v)
#else
#define AUTO(r,v) typeof(v) r = (v)
#endif
#define ALL(c) (c).begin() , (c).end()
#define EACH(it,c) for(AUTO(it,(c).begin());it != (c).end();++it)
#define LL long long
#define int LL
#define inf  ((int)1 << 54)
#define DIV 1000000007
#define QUICK_CIN ios::sync_with_stdio(false); cin.tie(0);
#define InitArr1(c,n) memset(&c[0],0,sizeof(int)*n)
#define InitArr2(c,n) memset(&c[0][0],0,sizeof(int)*n)
#define debug_input fstream cin("input.txt");ofstream cout("output.txt");

template<class T>
bool valid(T x, T w) { return 0 <= x&&x < w; }

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

using namespace std;

//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

bool ed[101][101];
int w, h;
int m[101][101];

bool dfs(int x, int y)
{
	auto num = m[x][y];
	bool led[101][101] = {false};

	if (ed[x][y])return false;

	stack<pair<pair<int,int>, pair<int,int>>> que;
	que.push({ { x,y },{-1,-1} });

	while (que.size()) {
		auto now = que.top();
		que.pop();

		if (led[now.first.first][now.first.second])
			return true;

		led[now.first.first][now.first.second] = true;
		ed[now.first.first][now.first.second] = true;

		REP(i, 4) {
			auto nx = now.first.first + dx[i];
			auto ny = now.first.second + dy[i];

			if (nx == now.second.first && ny == now.second.second)
				continue;

			if (valid(nx, h) && valid(ny, w) && num == m[nx][ny]) {
				que.push({ { nx,ny }, {now.first.first , now.first.second } });
			}
		}
	}
	return false;
}

signed main()
{
	QUICK_CIN;
	//debug_input;

	cin >> w >> h;

	REP(i, h) {
		REP(j, w) {
			cin >> m[i][j];
		}
	}
	REP(i, h) {
		REP(j, w) {
			if (dfs(i, j)) {
				cout << "possible" << endl;
				return 0;
			}
		}
	}
	cout << "impossible" << endl;
}
0