結果

問題 No.13 囲みたい!
ユーザー OnjuOnju
提出日時 2015-02-10 19:54:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,292 bytes
コンパイル時間 590 ms
コンパイル使用メモリ 57,820 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:16:30
合計ジャッジ時間 1,162 ms
ジャッジサーバーID
(参考情報)
judge2 / 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 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 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 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#define W_MAX 100
#define H_MAX 100
#define T_USED 10000
using namespace std;

enum Way
{
	None = 0,
	Right,
	Left,
	Up,
	Down
};

int W, H, M[H_MAX + 2][W_MAX + 2], t[H_MAX + 2][W_MAX + 2];


bool update(int x, int y, int m, Way from)
{
	if (M[y][x] == m && t[y][x] >= T_USED)
		return true;
	else if (M[y][x] == m)
		t[y][x] = T_USED;
	else
		return false;

	if (from != Left && update(x - 1, y, m, Right))
		return true;
	if (from != Right && update(x + 1, y, m, Left))
		return true;
	if (from != Up && update(x, y - 1, m, Down))
		return true;
	if (from != Down && update(x, y + 1, m, Up))
		return true;

	return false;
}

bool calc()
{
	for (int y = 1; y <= H; ++y)
		for (int x = 1; x <= W; ++x)
			if (t[y][x] != T_USED && update(x, y, M[y][x], None))
				return true;

	return false;
}

int main()
{
	cin >> W >> H;

	for (int y = 1; y <= H; ++y)
		for (int x = 1; x <= W; ++x)
		{
			cin >> M[y][x];
			t[y][x] = 0;
		}

	for (int y = 0; y <= H + 1; ++y)
	{
		M[y][0] = M[y][W + 1] = t[y][0] = t[y][W + 1] = 0;
	}
	for (int x = 0; x <= W + 1; ++x)
	{
		M[0][x] = M[H + 1][x] = t[0][x] = t[H + 1][x] = 0;
	}

	if (calc())
		cout << "possible" << endl;
	else
		cout << "impossible" << endl;

	return 0;
}
0