UVa 109 SCUD Busters (凸包面积&判断点是否在凸包内部)

262 篇文章 0 订阅
40 篇文章 0 订阅

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=45


套模板即可。


一道类似的题:LightOJ 1190 Sleepwalking


完整代码:

/*0.018s*/

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

struct P
{
	int x, y;
	P(int x = 0, int y = 0): x(x), y(y) {}
	bool read()
	{
		return ~scanf("%d%d", &x, &y);
	}
	bool operator < (const P& p) const ///加cosnt以便sort调用,其他函数不加const对速度没有影响
	{
		return x < p.x || x == p.x && y < p.y;
	}
	P operator + (P p)
	{
		return P(x + p.x, y + p.y);
	}
	P operator - (P p)
	{
		return P(x - p.x, y - p.y);
	}
	int dot(P p)
	{
		return x * p.x + y * p.y;
	}
	int det(P p)
	{
		return x * p.y - y * p.x;
	}
} a[105], ans[25][105];

int n, len, Size[25];
bool vis[25];

///求凸包
void convex_hull(int country)
{
	sort(a, a + n);
	len = 0;
	int i;
	for (i = 0; i < n; ++i)
	{
		while (len >= 2 && (ans[country][len - 1] - ans[country][len - 2]).det(a[i] - ans[country][len - 1]) <= 0)
			--len;
		ans[country][len++] = a[i];
	}
	int tmp = len;
	for (i = n - 2; i >= 0; --i)
	{
		while (len > tmp && (ans[country][len - 1] - ans[country][len - 2]).det(a[i] - ans[country][len - 1]) <= 0)
			--len;
		ans[country][len++] = a[i];
	}
	--len;
	ans[country][len] = ans[country][0]; ///ok
	Size[country] = len;
}

///点q在线段p1p2上
inline bool on_seg(P p1, P p2, P q)
{
	return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}

///点q在country内
bool in_area(int country, P q)
{
	int cnt = 0, k, d1, d2;
	for (int i = 0; i < Size[country]; ++i)
	{
		if (on_seg(ans[country][i], ans[country][i + 1], q)) return false;
		k = (ans[country][i + 1] - ans[country][i]).det(q - ans[country][i]);
		d1 = ans[country][i].y - q.y;
		d2 = ans[country][i + 1].y - q.y;
		if (k > 0 && d1 <= 0 && d2 > 0) ++cnt;
		if (k < 0 && d1 > 0 && d2 <= 0) --cnt;
	}
	return cnt != 0;
}

int area(int country)
{
	int sum = 0;
	for (int i = 0; i < Size[country]; ++i)
		sum += ans[country][i].det(ans[country][i + 1]);
	return sum;
}

int main()
{
	int country = 0, i;
	for (; scanf("%d", &n), n > 0; ++country)
	{
		for (i = 0; i < n; ++i) a[i].read();
		convex_hull(country);
	}
	int sumA = 0;
	P p;
	while (p.read())
		for (i = 0; i < country; ++i)
			if (!vis[i] && in_area(i, p))
			{
				vis[i] = true;
				sumA += area(i);
			}
	if (sumA & 1) printf("%d.50\n", sumA >> 1);
	else printf("%d.00\n", sumA >> 1);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值