aoj2201(極限情況)

/*
translation:
	有n個寶石,寶石爲圓形。給出現在有根金屬棒,靠近寶石距離m之內就會吸附上去。現在給出每顆寶石的信息,求用這個棒子一次
	能釣出最多多少個寶石?
solution:
	極限情況
	每個寶石可以看成兩個圓(本來的圓+半徑加上m的圓)。對這些圓形一一枚舉,即可求出各自的公切線(即金屬棒)。然後
	求出此時能吸附多少寶石。逐一更新即可。
note:
	# 爲何最後計算吸附多少寶石時用lamda表達式就能過,自己寫的函數就過不了?QAQ
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;
const int maxn = 100 + 5;
const double PI = acos(-1);

struct Point
{
    double x, y;
    Point(){}
    Point(double x_, double y_):x(x_),y(y_){}
};
typedef Point Vector;
int n;

struct Circle:public Point
{
    Point c;
    double r, m;
    Circle(){}
    Circle(Point c, double r, double m):c(c.x, c.y),r(r),m(m){}
    Point getPoint(double a) {
        return Point(c.x + cos(a) * r, c.y + sin(a) * r);
    }
};
vector<Circle> p;

Vector operator + (Vector a, Vector b)  { return Vector(a.x + b.x, a.y + b.y); }
Vector operator - (Point a, Point b)    { return Point(a.x - b.x, a.y - b.y); }
Vector operator * (Vector a, double p)  { return Vector(a.x * p, a.y * p); }
Vector operator / (Vector a, double p)  { return Vector(a.x / p, a.y / p); }

bool operator < (const Point& a, const Point& b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

const double eps = 1e-12;
int dcmp(double x)
{
    if(fabs(x) < eps)    return 0;
    else                return x < 0 ? -1 : 1;
}

bool operator == (const Point& a, const Point& b)
{
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

double dot(Vector a, Vector b)      { return a.x * b.x + a.y * b.y; }
double length(Vector a)             { return sqrt(dot(a, a)); }
double angle(Vector a, Vector b)    { return acos(dot(a, b) / length(a) / length(b)); }
double angle(Vector v)              { return atan2(v.y, v.x); }
double cross(Vector a, Vector b)    { return a.x * b.y - b.x * a.y; }
double dist(Point p1,Point p2)      { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); }

double distToLine(Point p, Point a, Point b) {
	Vector v1 = b - a, v2 = p - a;
	return fabs(cross(v1, v2)) / length(v1);
}

int getTangents(Circle A, Circle B, vector<Point>& a, vector<Point>& b)
{
	int cnt = 0;
	if(A.r < B.r) { swap(A, B); swap(a, b); }
	int d2 = (A.c.x - B.c.x)*(A.c.x - B.c.x) + (A.c.y - B.c.y)*(A.c.y - B.c.y);
	int rdiff = A.r - B.r;
	int rsum = A.r + B.r;
	if(d2 < rdiff * rdiff)	return 0;

	double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
	if(d2 == 0 && A.r == B.r)	return -1;
	if(d2 == rdiff * rdiff) {
		a.push_back(A.getPoint(base));
		b.push_back(B.getPoint(base));
		return 1;
	}

	//有共切線
	double ang = acos((A.r - B.r) / sqrt(d2));
	a.push_back(A.getPoint(base + ang));	b.push_back(B.getPoint(base + ang));
	a.push_back(A.getPoint(base - ang));	b.push_back(B.getPoint(base - ang));

	if(d2 == rsum * rsum) {
		a.push_back(A.getPoint(base));
		b.push_back(B.getPoint(PI + base));
	} else if(d2 > rsum * rsum) {
		double ang = acos((A.r + B.r) / sqrt(d2));
		a.push_back(A.getPoint(base + ang));	b.push_back(B.getPoint(PI + base + ang));
		a.push_back(A.getPoint(base - ang));	b.push_back(B.getPoint(PI + base - ang));
	}
	return a.size();
}

int cmp(double a, double b)
{
	const double diff = a - b;
	if (fabs(diff) < eps)	return 0;
	else if (diff < 0)		return -1;
	else					return 1;
}

int main()
{
	//freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n)) {
		if(n == 0)	break;
		p.clear();
		double x, y, r, m;
		for(int i = 0; i < n; i++) {
			scanf("%lf%lf%lf%lf", &x, &y, &r, &m);
			p.push_back(Circle(Point(x, y), r, m));
		}

		vector<Point> a, b;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) if(i != j) {
				Circle A1 = p[i], A2 = p[i];
				Circle B1 = p[j], B2 = p[j];
				B2.r += B2.m;	A2.r += A2.m;

				getTangents(A1, B1, a, b);
				getTangents(A1, B2, a, b);
				getTangents(A2, B1, a, b);
				getTangents(A2, B2, a, b);
			}
		}

		int num = a.size(), ans = 1;
		for(int i = 0; i < num; i++) {
			int res = count_if(p.begin(), p.end(), [&](const Circle& C) {
								return cmp(distToLine(C.c, a[i], b[i]), C.r) >= 0
								&& cmp(distToLine(C.c, a[i], b[i]), C.r + C.m) <= 0; });
			ans = max(ans, res);
		}
		printf("%d\n", ans);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章