2022.5.20 AcWing每日一题

DFS
不是选出三条直线再检测,而是在运行的过程中,如果遇到了没有被前面的情况,选取垂直或平行的直线。

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

typedef pair<int, int> PII;

int n;
vector<PII> cows;
map<int, bool> cx, cy;

bool dfs(int cur, int use) {
	if (use > 3)
		return false;
	if (cur == n)
		return true;

	int x = cows[cur].first, y = cows[cur].second;
	if (cx[x] || cy[y])
		return dfs(cur + 1, use);

	bool can = 0;
	if (!cx[x])
		cx[x] = true, can |= dfs(cur + 1, use + 1), cx[x] = false;
	if (!cy[y])
		cy[y] = true, can |= dfs(cur + 1, use + 1), cy[y] = false;

	return can;
}


int main() {
	scanf("%d", &n);
	for (int i = 1, x, y; i <= n && cin >> x >> y; i++)
		cows.push_back({x, y});
	cout << dfs(0, 0) << endl;
	return 0;
}
posted @ 2022-05-20 10:36  superPG  阅读(14)  评论(0编辑  收藏  举报