UVA 11930 - Rectangles(2-sat + 计算几何)

UVA 11930 - Rectangles

题目链接

题意:给定一些矩形,每个在两条对角线选一条,保证全部不相交,问可不可行(这题有坑啊,矩形不一定平行坐标轴。。。)

思路:2-sat,主对角线为true,副对角线为false,枚举两个矩形的每条对角线,利用叉积判相交,如果相交就加一条边进去,最后2-sat判定即可

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXNODE = 1005;

struct TwoSet {
	int n;
	vector<int> g[MAXNODE * 2];
	bool mark[MAXNODE * 2];
	int S[MAXNODE * 2], sn;

	void init(int tot) {
		n = tot * 2;
		for (int i = 0; i < n; i += 2) {
			g[i].clear();
			g[i^1].clear();
		}
		memset(mark, false, sizeof(mark));
	}

	void add_Edge(int u, int uval, int v, int vval) {
		u = u * 2 + uval;
		v = v * 2 + vval;
		g[u^1].push_back(v);
		g[v^1].push_back(u);
	}

	void delete_Edge(int u, int uval, int v, int vval) {
		u = u * 2 + uval;
		v = v * 2 + vval;
		g[u^1].pop_back();
		g[v^1].pop_back();
	}

	bool dfs(int u) {
		if (mark[u^1]) return false;
		if (mark[u]) return true;
		mark[u] = true;
		S[sn++] = u;
		for (int i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			if (!dfs(v)) return false;
		}
		return true;
	}

	bool solve() {
		for (int i = 0; i < n; i += 2) {
			if (!mark[i] && !mark[i + 1]) {
				sn = 0;
				if (!dfs(i)){
					for (int j = 0; j < sn; j++)
						mark[S[j]] = false;
					sn = 0;
					if (!dfs(i + 1)) return false;
				}
			}
		}
		return true;
	}
} gao;

typedef long long ll;

struct Point {
	ll x, y;
	Point() {}
	Point(ll x, ll y) {
		this->x = x;
		this->y = y;
	}
	void read() {
		scanf("%lld%lld", &x, &y);
	}
};

int xmul(Point a, Point b, Point c) {
	ll ans = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
	if (ans == 0) return 0;
	if (ans > 0) return 1;
	return -1;
}

bool judge(Point a1, Point b1, Point a2, Point b2) {
	if (min(a1.x, b1.x) <= max(a2.x, b2.x) &&
		min(a1.y, b1.y) <= max(a2.y, b2.y) &&
		min(a2.x, b2.x) <= max(a1.x, b1.x) &&
		min(a2.y, b2.y) <= max(a1.y, b1.y) &&
		xmul(a1, a2, b1) * xmul(a1, b2, b1) <= 0 &&
		xmul(a2, a1, b2) * xmul(a2, b1, b2) <= 0)
		return true;
	else
		return false;
}

const int N = 1005;
const ll INF = 0x3f3f3f3f;

bool cmp(Point a, Point b) {
	if (a.x == b.x) return a.y < b.y;
	return a.x < b.x;
}

int n;
struct Rec {
	Point a[2], b[2];
	Rec() {}
	Rec(ll l, ll r, ll u, ll d) {
		a[0] = Point(l, u); b[0] = Point(r, d);
		a[1] = Point(r, u); b[1] = Point(l, d);
	}
	void init() {
		Point tmp[4];
		for (int i = 0; i < 4; i++)
			tmp[i].read();
		sort(tmp, tmp + 4, cmp);
		b[1] = tmp[0];
		a[0] = tmp[1];
		b[0] = tmp[2];
		a[1] = tmp[3];
		/*
		if (tmp[0].x != tmp[1].x) while(1);
		if (tmp[2].x != tmp[3].x) while(1);
		if (tmp[0].y != tmp[2].y) while(1);
		if (tmp[1].y != tmp[3].y) while(1);
		*/
	}
} rec[N];

int main() {
	while (~scanf("%d", &n) && n) {
		gao.init(n);
		//ll x, y;
		//ll l, r, u, d;
		for (int i = 0; i < n; i++) {
			//l = d = INF; r = u = -INF;
			/*for (int j = 0; j < 4; j++) {
				scanf("%lld%lld", &x, &y);
				l = min(l, x); r = max(r, x);
				d = min(d, y); u = max(u, y);
			}
			rec[i] = Rec(l, r, u, d);*/
			rec[i].init();
			for (int j = 0; j < i; j++) {
				for (int x = 0; x < 2; x++)
					for (int y = 0; y < 2; y++) {
						if (judge(rec[i].a[x], rec[i].b[x], rec[j].a[y], rec[j].b[y]))
							gao.add_Edge(i, x, j, y);
					}
			}
		}
		printf("%s\n", gao.solve() ? "YES" : "NO");
	}
	return 0;
}
时间: 2024-08-29 10:37:37

UVA 11930 - Rectangles(2-sat + 计算几何)的相关文章

UVA 11178 Morley&#39;s Theorem 计算几何

计算几何: 最基本的计算几何,差积  旋转 Morley's Theorem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the line

uva 11796 Dog Distance (计算几何-点和直线)

C Dog Distance Input Standard Input Output Standard Output Two dogs, Ranga and Banga, are running randomly following two different paths. They both run for T seconds with different speeds. Ranga runs with a constant speed of R m/s, whereas Banga runs

uva 10652 Board Wrapping (计算几何-凸包)

Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The small sawmill in Mission, British Columbia, has developed a brand new way of packaging boards for drying. By fixating the boards in special moulds, the b

UVA - 11437 - Triangle Fun (计算几何~)

UVA - 11437 Triangle Fun Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem A Triangle Fun Input: Standard Input Output: Standard Output In the picture below you can see a triangle ABC. Point D, E

UVA - 11796 - Dog Distance (计算几何~)

不得不感叹,计算几何真是太美丽了!! AC代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x) , y(y) { } }; t

UVA - 11646 - Athletics Track (计算几何~)

题目地址:点这里 思路:计算几何入门题,首先,两个圆弧是同一个圆的,所以这个圆是矩形的外接圆,那么矩形中心就是圆心,由长宽算出角度和半径(这时用单位长度表示),再算出一个单位长度的实际长度,从而得出长和宽 AC代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std;

UVA 10522 - Height to Area(计算几何)

这题就海伦公式带进去就可以了.. 要注意的是,这题的样例,是输入n次错误的输入才停止..,输入的可能是负数. 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-8; int t; double Ha, Hb, Hc; int dcmp(double x) { if (

UVa 1643 Angle and Squares (计算几何)

题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得到答案. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #incl

UVA 12300 - Smallest Regular Polygon(计算几何)

思路:找对点最优,奇数情况下,半径要另外算 大概这么一个图,偶数就是距离的一半,奇数的话,做一个正2N边形,那么半径就可以利用三角函数算出来了 然后有了外切圆半径,就能算面积了,为sin(2pi / n) * r * r * n / 2 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double