結果

問題 No.13 囲みたい!
ユーザー hotpepsihotpepsi
提出日時 2015-03-16 23:21:47
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,152 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 64,928 KB
実行使用メモリ 814,456 KB
最終ジャッジ日時 2023-09-11 08:30:14
合計ジャッジ時間 3,995 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 && (nx != prevx || ny != prevy) && d[y][x] == d[ny][nx]) {
			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; 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