【USACO3.4.1】闭合的栅栏

51 篇文章 0 订阅

第一问:其实不用考虑,没这个情况。考虑的话,就考虑所有的边之间是否有交点即可。


第二问:

从观测点发射一个视野,看到一个栅栏。


这个发射的直线视野,一定和一个栅栏的两个顶点有关系。 能看到顶点,才有可能看到这个栅栏。


那么我们可以把从观测点,到顶点连线。 得到直线L1


然后把直线L1,略倾斜一个小角度,然后用来check所有的其他栅栏, 看最早看到的栅栏是什么。

TIP:  视野会被顶点挡住,也就是说,看到顶点,并不表示看到顶点所在的栅栏,但是这个视线,一定不能穿过顶点往后看了。


这个方法还是挺快的


/*
TASK:fence4
LANG:C++
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#define max(a, b) ((a)>(b)?(a):(b))
#define min(a, b) ((a)<(b)?(a):(b))
const int max_n = 200 + 30;
int n;
 
struct node
{
	double x, y;
}a[max_n], zero;
 
inline double direction(node A1, node A2, node B1, node B2) //A1A2向量 X B1B2向量
{
	return (A2.x - A1.x)*(B2.y - B1.y) - (A2.y - A1.y) * (B2.x - B1.x);
}
 
inline bool on_segment(node A1, node A2, node K)//K是否在A1A2上
{
	if (min(A1.x, A2.x) <= K.x && K.x <= max(A1.x, A2.x) && min(A1.y, A2.y) <= K.y && K.y <= max(A1.y, A2.y))	return true;
	return false;
}
 
inline int segments_intersect(node A1, node A2, node B1, node B2, int flag=1)//A1A2 B1B2是否有交点,有点交也算
{
	double d1 = direction(A1, A2, A1, B1);
	double d2 = direction(A1, A2, A1, B2);
	double d3 = direction(B1, B2, B1, A1);
	double d4 = direction(B1, B2, B1, A2);
	if (d1 * d2 <0 && d3 * d4 <0)	return true;
	if (flag) //如果要求在点上相交也算
	{	
		if (!d1 && on_segment(A1, A2, B1))	return true;	
		if (!d2 && on_segment(A1, A2, B2))	return true;
		if (!d3 && on_segment(B1, B2, A1))	return true;
		if (!d4 && on_segment(B1, B2, A2))	return true;
	}
	return false;
}
 
void init()
{
	scanf("%d", &n);
	scanf("%lf%lf", &a[0].x, &a[0].y);
	for (int i = 1; i <= n; ++ i)	
	{
		scanf("%lf%lf", &a[i].x, &a[i].y);
		a[i].x -= a[0].x;
		a[i].y -= a[0].y;
	}
//	a[0].x = a[0].y = 0;
	a[n + 1] = a[1];
	for (int i = 1; i <= n; ++ i)
		//a[i]a[i + 1] 是一条边
		for (int j = i + 2; j <= n; ++ j)
		{
			//cout<<"!"<<i<<" " <<j<<endl;
			if (i == 1 && j == n)	continue;
			if (segments_intersect(a[i], a[i + 1], a[j], a[j + 1]))
			{
				printf("NOFENCE\n");
				exit (0);
			}
		}
	zero.x = zero.y = 0;
}
 
 
bool ans[200 + 20]={false};
 
void check(node K)//OK 是否和别人有交点
{
	double min_dis = 100000000.0;
	double A = K.y; 
	double B = -K.x;
	int save=0;
	for (int i = 1; i <= n; ++ i)
	{
		if (!segments_intersect(zero, K, a[i], a[i + 1], 0) && segments_intersect(zero, K, a[i], a[i + 1]))
		{
			//只是在顶点相交的话,也会卡视野,视野更新,但是不算
			double ta = a[i + 1].y - a[i].y;
			double tb = a[i].x - a[i + 1].x;
			double tc = a[i].y*(a[i + 1].x - a[i].x) - a[i].x * (a[i + 1].y - a[i].y);
			double ty = (A * tc) / (A * tb - ta * B);
			double tx = (- B * ty) / A;
			if (min_dis > tx * tx + ty * ty)	min_dis = tx * tx + ty * ty;
			continue;
		}
		if (!segments_intersect(zero, K, a[i], a[i + 1], 0))	continue; //没交点
		//如果有交点,要求2直线距离
		double ta = a[i + 1].y - a[i].y;
		double tb = a[i].x - a[i + 1].x;
		double tc = a[i].y*(a[i + 1].x - a[i].x) - a[i].x * (a[i + 1].y - a[i].y);
		double ty = (A * tc) / (A * tb - ta * B);
		double tx = (- B * ty) / A;
		if (min_dis > tx * tx + ty * ty)
		{
			min_dis = tx * tx + ty * ty;
			save = i;
		}
	}
	ans[save] = true;
}
 
 
void doit()
{
	for (int i = 1; i <= n; ++ i)
	{
		node dir, tmp;	
		dir.x = a[i].x * 1000;
		dir.y = a[i].y * 1000;
		tmp = dir;
		dir = tmp;
		dir.x = tmp.x + 0.1;
		check(dir);
 
		dir = tmp;
		dir.x = tmp.x - 0.1;
		check(dir);
 
		dir = tmp;
		dir.y = tmp.y - 0.1;
		check(dir);
 
		dir = tmp;
		dir.y = tmp.y + 0.1;
		check(dir);
	}
//	cout<<a[0].x<<"! "<<a[0].y<<endl;
	int output = 0;
	for (int i = 1; i <= n; ++ i)
		if (ans[i])	++output;
	printf("%d\n", output);
	for (int i = 1; i <= n - 2; ++ i)
		if (ans[i])	printf("%.0lf %.0lf %.0lf %.0lf\n", a[i].x + a[0].x, a[i].y + a[0].y, a[i + 1].x + a[0].x, a[i + 1].y + a[0].y);
	if (ans[n])	printf("%.0lf %.0lf %.0lf %.0lf\n", a[n + 1].x + a[0].x, a[n + 1].y + a[0].y, a[n].x + a[0].x, a[n].y + a[0].y);
	if (ans[n - 1])	printf("%.0lf %.0lf %.0lf %.0lf\n", a[n - 1].x + a[0].x, a[n - 1].y + a[0].y, a[n].x + a[0].x, a[n].y + a[0].y);
}
 
int main()
{
	//freopen("fence4.in","r",stdin);
	//freopen("fence4.out","w",stdout);
	init();	
	doit();
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值