URAL 1111. Squares

我当水题刷的,结果被屠了。可怜

极度无语。。。

就是问你这个点到所有正方形的最小距离,然后按距离排序,排完输出ID即可。

水题是吧?

做了好久啊啊啊!!输入的是对角线两点坐标,我自作聪明地认为边平行坐标轴了啊!!其实木有啊!!!以前都遇到过类似的输入,需要找到另外俩点啊!!!

一直WA,DISCUSS上的一组数据让我不知道错在哪儿了!!!用几何画板把一个正方形画出来了,那是正方形么!!三点都共线了!!肿么可能!!!我的模板可是经过千锤百炼的!!!一输出,发现根本不是初始的坐标!!!再一看,数组开小了!!!

中间操作至少要开6个点的坐标啊!!!!

为啥会覆盖掉呢,经研究,因为内存是连续的,所以数组没开那么大的话,它就指向下一个结构体的内存里了,也就是把下个结构体里的数组的前面的点给覆盖掉了!!!我了个崩溃T T 。。

好吧,吃一堑,长一智。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=s; i<t; i++)
#define BUG puts("here!!!")
#define STOP system("pause")

using namespace std;

const int MAX = 55;
struct point {
	double x, y;
	void P(double xx, double yy)
	{
		x = xx; y = yy;
	}
	void get()
	{
		scanf("%lf%lf", &x, &y);
	}
};
struct square{ point p[6];};
point c;
square s[MAX];
struct NODE{int id; double len;};
NODE node[MAX];
const double eps = 1e-6;
const double pi = acos(-1.0);
const double inf = 1e30;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
double disp2p(point a,point b) //  a b 两点之间的距离 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 顺时针是正 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}

double disp2seg(point p,point l1,point l2)
{
	point t = p;
	t.x += l1.y - l2.y;  t.y += l2.x - l1.x;
	if( dyd(crossProduct(l1,t,p)*crossProduct(l2,t,p),0.0) ) //包括点和线段共线 
		return xy(disp2p(p,l1),disp2p(p,l2)) ? disp2p(p,l1) : disp2p(p,l2);
	return fabs( crossProduct(p,l1,l2) )/disp2p(l1,l2);
}

bool in(point a,point *p)
{
	int n = 4;
	p[n] = p[0]; p[n+1] = p[1];
	for(int i=0; i<n; i++)
		if( xy(crossProduct(p[i],p[i+1],a)*
			crossProduct(p[i+1],p[i+2],a),0.0) )
			return false;
	return true;
}

double solve(point a, point *p)
{
	double mmin = inf;
	p[4] = p[0];
	FOR(i, 0, 4)
	{
		double len = disp2seg(a, p[i], p[i+1]);
		if( xy(len, mmin) )
			mmin = len;
	}
	return mmin;
}

bool cmp(NODE a,NODE b)
{
	if( fabs(a.len-b.len) <= 1e-14 )
		return a.id < b.id;
	return xy(a.len, b.len);
}

point Rotate(double ang, point a, point b)
{    
	b.x -= a.x; b.y -= a.y;
    point c;
    c.x = b.x * cos(ang) - b.y * sin(ang) + a.x;
    c.y = b.x * sin(ang) + b.y * cos(ang) + a.y;
    return c;
}

int main()
{
	int n;
	point a, c;
	
	scanf("%d", &n);
	
	FOR(i, 0, n)
	{
		a.get(); c.get();
		point mid;
		mid.P((a.x + c.x)/2, (a.y + c.y)/2);
		s[i].p[0] = a;
		s[i].p[1] = Rotate(pi/2, mid, a);
		s[i].p[2] = c;
		s[i].p[3] = Rotate(pi/2, mid, c);
	}
	c.get();

	FOR(i, 0, n)
	{
		node[i].id = i+1;
		if( in(c, s[i].p) )
			node[i].len = 0;
		else
			node[i].len = solve(c, s[i].p);
	}

	sort(node, node+n, cmp);
	
	FOR(i, 0, n)
		printf(i == n-1 ? "%d\n" : "%d ", node[i].id);

return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值