結果

問題 No.13 囲みたい!
ユーザー ldsybldsyb
提出日時 2017-07-18 20:50:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,056 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 77,532 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 01:59:37
合計ジャッジ時間 1,646 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

constexpr int dd[] = {0, 1, 0, -1, 0};

bool inside(int a, int b, int alim, int blim){
	return 0 <= a && a < alim && 0 <= b && b < blim;
}

bool solve(vector<vector<int>> &vec, vector<vector<bool>> &done, int a, int b, int pre_a = -1, int pre_b = -1){
	done[a][b] = true;
	bool f = false;
	for(int i = 0; i < 4; i++){
		int next_a = a + dd[i], next_b = b + dd[i + 1];
		if(next_a == pre_a && next_b == pre_b) continue;
		if(inside(next_a, next_b, vec.size(), vec[0].size()) && vec[a][b] == vec[next_a][next_b]){
			if(done[next_a][next_b]) return true;
			f |= solve(vec, done, next_a, next_b, a, b);
		}
	}
	return f;
}

int main(){
	int w, h;
	cin >> w >> h;
	vector<vector<bool>> done(h, vector<bool>(w));
	vector<vector<int>> a(h, vector<int>(w));
	for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) cin >> a[i][j];
	for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) if(!done[i][j] && solve(a, done, i, j)){
		cout << "possible" << endl;
		return 0;
	}
	cout << "impossible" << endl;
}
0