結果

問題 No.13 囲みたい!
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-16 00:12:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 895 bytes
コンパイル時間 1,905 ms
コンパイル使用メモリ 157,792 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:18:05
合計ジャッジ時間 1,743 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 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 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 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

int H, W;
int board[100][100];
bool check[100][100];

bool ok(int y, int x){
	return y >= 0 && x >= 0 && y < H && x < W;
}

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

bool dfs(int y, int x, int pre){
	check[y][x] = true;
	for (int k = 0; k < 4; k++)
	{
		if (pre == k) continue;
		int ny = y + dy[k];
		int nx = x + dx[k];
		if (!ok(ny, nx)) continue;
		if (board[y][x] != board[ny][nx]) continue;
		if (check[ny][nx]) return true;
		if (dfs(ny, nx, (k + 2) % 4)) return true;
	}
	return false;
}

int main() {
	cin >> W >> H;
	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			cin >> board[i][j];
		}
	}
	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			if (check[i][j]) continue;
			if (dfs(i, j, -1)){
				cout << "possible" << endl;
				return 0;
			}
		}
	}
	cout << "impossible" << endl;
}



0