結果

問題 No.13 囲みたい!
ユーザー furonfuron
提出日時 2023-05-30 15:48:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,751 bytes
コンパイル時間 1,529 ms
コンパイル使用メモリ 132,620 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 00:39:40
合計ジャッジ時間 2,359 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
//const int MOD = 1000000007;
const int MOD = 998244353;

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

bool dfs(vvi& a, vvb& visit, int y, int x, int dir, int h, int w) {
	visit[y][x] = true;
	bool ret = false;
	for (int i = 0; i < 4; i++) {
		if (i == (dir + 2) % 4) continue;
		
		int x2 = x + dx[i];
		int y2 = y + dy[i];
		if (y2 < 0 || y2 > h - 1 || x2 < 0 || x2 > w - 1) continue;
		if (a[y2][x2] != a[y][x]) continue;

		if (visit[y2][x2]) return true;
		
		if (!visit[y2][x2]) {
			ret = dfs(a, visit, y2, x2, i, h, w);
			if (ret) return true;
		}
	}
	return false;
}

int main() {
	int h, w;
	cin >> w >> h;
	vvi a(h, vi(w));
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> a[y][x];
		}
	}
	vvb visit(h, vb(w, false));
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			if (visit[y][x]) continue;
			if (dfs(a, visit, y, x, -11, h, w)) {
				cout << "possible" << endl;
				return 0;
			}
		}
	}
	cout << "impossible" << endl;
	
	return 0;
}
0