问题 E: Fruit Slicer n个圆求一条直线经过圆的数目最多

问题 E: Fruit Slicer

时间限制: 1 Sec  内存限制: 128 MB
提交: 25  解决: 4
[提交] [状态] [命题人:admin]

题目描述

John, a student who is taking the game development course, recently developed a mobile game called Fruit Slicer for his coursework. In the game the player slices fruits that are throw into the air by swiping the touch screen. However the game is quite simple because John was not able to write code for the geometry required for a more complex version. In the game each slice is a straight line of infinite length, and all fruits have the same shape of a circle with unit radius. The figure shows a cool snapshot of John’s game.
John introduces his game to his best friend Sean, who soon gets bored of playing the simple game. But as a teaching assistant of the algorithm course, Sean decides to turn the game into a homework assignment. He asks the students in the algorithms course to write a program that can compute the best slice at any given moment of the game. Given the locations of the fruits, the program should determine the maximum number of fruits that can be sliced with a single straight-line swipe.

As a student in Sean’s class, you are now the one who is facing this challenge.

 

输入

The first line has a single integer n (1≤n≤100). The next n lines each have two real numbers giving the x and y coordinates of a fruit. All coordinates have an absolute value no larger than 104 and are given with exactly two digits after the decimal point. Fruits may overlap.

 

输出

Output the maximum number of fruits that can be sliced with one straight-line swipe. A swipe slices a fruit if the line intersects the inner part or the boundary of the fruit.

 

样例输入

复制样例数据

5
1.00 5.00
3.00 3.00
4.00 2.00
6.00 4.50
7.00 1.00

样例输出

4

题解:最优解肯定是两个圆的公切线,这个可以想象一下,然后就是枚举两个圆就可以了

#include <iostream>
#include <cmath>
using namespace std;
  
const double eps = 1e-6;
const double PI = acos(-1.0);
int sgn(double x){
    if(fabs(x) < eps)return 0;
    if(x < 0)return-1;
    else return 1;
}
double sqr(double x){return x*x;}
struct Point{
    double x, y;
Point(){}
    Point(double _x,double _y){
        x = _x;
        y = _y;
    }
    bool operator == (Point b)const{
        return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;
    }
    bool operator < (Point b)const{
        return sgn(x-b.x)== 0-sgn(y-b.y)?0:x<b.x;
    }
    Point operator-(const Point &b)const{
        return Point(x-b.x,y-b.y);
    }
    //叉积
    double operator ^(const Point &b)const{
        return x*b.y-y*b.x;
    }
    //点积
    double operator *(const Point &b)const{
        return x*b.x + y*b.y;
    }
    //返回长度
    double len(){
        return hypot(x,y);//库函数
    }
    //返回长度的平方
    double len2(){
        return x*x + y*y;
    }
    //返回两点的距离
    double distance(Point p){
        return hypot(x-p.x,y-p.y);
    }
    Point operator +(const Point &b)const{
        return Point(x+b.x,y+b.y);
    }
    Point operator *(const double &k)const{
        return Point(x*k,y*k);
    }
    Point operator /(const double &k)const{
        return Point(x/k,y/k);
    }
};
  
struct Line{
    Point s,e;
Line(){}
    Line(Point _s,Point _e){
        s = _s;
        e = _e;
    }
      
    //求线段长度
    double length(){
        return s.distance(e);
    }
   
    double dispointtoline(Point p){
        return fabs((p-s)^(e-s))/length();
    }
};
struct Circle{
    Point p;
    double r;
Circle(){}
    Circle(Point _p,double _r){
        p = _p;
        r = _r;
    }
    // 过一点知道角度和距离 求另一点 
    Point getpoint(double b) { 
        return Point(p.x + cos(b)*r, p.y+sin(b)*r);
    }
      
    int relationline(Line v){
        double dst = v.dispointtoline(p);
        if(sgn(dst-r) < 0)return 2;
        else if(sgn(dst-r) == 0)return 1;
        return 0;
    }
};
  
// 求两圆的公切线
  
int getTan(Circle A, Circle B, Point*  va ,Point* vb) {
    int cnt = 0;
    if(A.r < B.r) {swap(A, B); swap(va, vb); }
    double d = (A.p-B.p).len();
    double rdif = A.r - B.r, rsum = A.r+B.r;
    //内含,没有公切线
    if(sgn(d-rdif)<0)return 0;
    //内切,有一条公切线
    double base = atan2(B.p.y - A.p.y, B.p.x - A.p.x);
    if(sgn(d) == 0 && sgn(A.r - B.r) == 0) { // 两圆重合 
        va[0] = A.p;
        vb[0] = A.getpoint(PI); 
        return 1;
    }
    if(sgn(d - rdif ) == 0){
        va[cnt] = A.getpoint(base); vb[cnt] = B.getpoint(base); cnt++;
        return cnt;
    }
    //一定有两条外公切线
    double th = acos((A.r-B.r)/d);
    va[cnt] = A.getpoint(base + th); vb[cnt] = B.getpoint(base + th); cnt++;
    va[cnt] = A.getpoint(base - th); vb[cnt] = B.getpoint(base - th); cnt++;
    //可能有一条公切线
    if(sgn(d - rsum) == 0){
        va[cnt] = A.getpoint(base); vb[cnt] = B.getpoint(base + PI); cnt++;
    }
    else if(sgn(d - rsum) > 0){
        double th2 = acos((A.r + B.r) / d);
        va[cnt] = A.getpoint(base + th2); vb[cnt] = B.getpoint(base + th2 + PI); cnt++;
        va[cnt] = A.getpoint(base - th2); vb[cnt] = B.getpoint(base - th2 + PI); cnt++;
    }
    return cnt;
}
Circle cir[110];
int n; 
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%lf %lf", &cir[i].p.x, &cir[i].p.y);
        cir[i].r = 1;
    }
    Point p1[5], p2[5];
    int cnt ;
    int ans = 0, tmp;
    Line Li;
    for(int i = 1; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            cnt = getTan(cir[i], cir[j], p1, p2);
        //  cout << cnt << endl;
            for(int k = 0; k < cnt; k++) {
                tmp = 0;
                Li = Line(p1[k], p2[k]);
            //  cout << p1[k].x << " " << p1[k].y << " " << p2[k].x << " " << p2[k].y << endl;
                for(int l = 1; l <= n; l++) {
                    if(cir[l].relationline(Li)) tmp++;
                }
                ans = max(ans, tmp);
            }
        }
    }
    if(n == 1) ans = 1;
    printf("%d\n", ans);
    return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值