結果

問題 No.13 囲みたい!
ユーザー hotpepsihotpepsi
提出日時 2015-03-16 23:26:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 202 ms / 5,000 ms
コード長 1,181 bytes
コンパイル時間 479 ms
コンパイル使用メモリ 66,284 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:17:03
合計ジャッジ時間 1,212 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

using namespace std;

int W, H;
int d[100][100];
int vis[100][100];
int sx, sy;

bool dfs(int x, int y, int prevx, int prevy) {
	vis[y][x] = 1;
	if (x == sx && y == sy) {
		return true;
	}
	int dx[] = { -1, 1, 0, 0 };
	int dy[] = { 0, 0, -1, 1 };
	for (int dir = 0; dir < 4; ++dir) {
		int nx = x + dx[dir], ny = y + dy[dir];
		if (nx >= 0 && nx < W && ny >= 0 && ny < H && !vis[ny][nx] && d[y][x] == d[ny][nx] && (nx != prevx || ny != prevy)) {
			if (dfs(nx, ny, x, y)) {
				return true;
			}
		}
	}
	return false;
}

int main(int argc, char *argv[]) {
	string s;
	getline(cin, s);
	stringstream ss(s);
	ss >> W >> H;
	for (int i = 0; i < H; ++i) {
		getline(cin, s);
		stringstream ss(s);
		for (int j = 0; j < W; ++j) {
			ss >> d[i][j];
		}
	}

	bool possible = false;
	for (int i = 0; i < H; ++i) {
		for (int j = 1; !possible && j < W; ++j) {
			if (d[i][j - 1] == d[i][j]) {
				memset(vis, 0, sizeof(vis));
				sx = j - 1, sy = i;
				if (dfs(j, i, j - 1, i)) {
					possible = true;
				}
			}
		}
	}

	cout << (possible ? "possible" : "impossible") << endl;
	return 0;
}
0