HDU 3952(计算几何)

来源:互联网 发布:美国经济数据在哪查看 编辑:程序博客网 时间:2024/04/28 13:50

问题描述:

Fruit Ninja is a popular classic game. During the game, fruits will up to the air, and your aim is cut as more fruits as possible with a line. 







Even if the line touch a point of a fruit, the fruit also be cut.

Input

The first line is a number T(1<=T<=30), represents the number of case. The next T blocks follow each indicates a case. 
The first line of each case contains one integer N (1<=N<=10) 
Then N lines follow, each line contains a integer K(3<=K<=10), represent the number points of the fruit, then K*2 integers follow, each two integers represent one point of the fruit.(with anticlockwise order) 
I promise all fruits are convex polygon, and any two fruit have no common point.

Output

For each case, output the number of case and the maximum fruits you could cut with a line.(as shown in the sample output)

Sample Input

233 0 0 1 0 1 13 1 2 2 1 2 23 3 1 3 0 4 034 0 0 1 0 1 1 0 14 2 0 3 0 3 1 2 14 0 99 1 99 1 100 0 100
Sample Output

Case 1: 3Case 2: 2
题目题意:题目给我们n个图形,每个图形给我们k个点(保证图形是凸图形),问一条直线最多可以经过多少个图形。

题目分析:我们假象一下,我们有一条直线现在经过了M个图形,那么当我们平移导致经过M-1个图形时,肯定会经过一个图形的某一顶点,那么经过M个图形的临界情况就是经过某俩个图形的顶点,然后再经过其他图形。我们枚举这个俩个点,这就是那条直线,然后去判断其他图形是否被这条直线穿过。


代码如下:

#include<iostream>#include<cstdio>#include<cmath>#include<cstdio>#include<vector>using namespace std;struct note{    int x,y;};int n;vector<struct note> vec[20];int cross(note a,note n,note m)//叉乘判断点a在直线nm的上方还是下方{    return (a.x-n.x)*(m.y-n.y)-(a.y-n.y)*(m.x-n.x);}bool check(note a,note b,note n,note m){    return cross(a,n,m)*cross(b,n,m)<=0;//如果积为负则必有上下俩点,为0则有点在线上}int solve(struct note a,struct note b,int mm,int nn){    int ans=2;//本身就经过俩个图形所以开始为2    for (int i=0;i<n;i++) {        if (i==mm||i==nn) continue;        vector <struct note> s;        s=vec[i];        s.push_back(vec[i][0]);        for (int j=0;j<s.size()-1;j++) {            if (check(s[j],s[j+1],a,b)) {//去判断是否经过其他图形,枚举它的线段是否与直线相交                ans++;                break;            }        }    }    return ans;}int main(){    int t,icase=1;    scanf("%d",&t);    while (t--) {        scanf("%d",&n);        for (int i=0;i<n;i++) {            int k;            scanf("%d",&k);            for (int j=0;j<k;j++) {                struct note a;                scanf("%d%d",&a.x,&a.y);                vec[i].push_back(a);            }        }        if (n==1) {//n=1是特判一下            printf("Case %d: %d\n",icase++,1);            continue;        }        int ans=0;        for (int i=0;i<n-1;i++) {//枚举任意俩个图形的所有的顶点            for (int j=0;j<vec[i].size();j++) {                for (int k=i+1;k<n;k++) {                    for (int l=0;l<vec[k].size();l++) {                        int res=solve(vec[i][j],vec[k][l],i,k);                        ans=max(ans,res);                    }                }            }        }        for (int i=0;i<n;i++) vec[i].clear();        printf("Case %d: %d\n",icase++,ans);    }    return 0;}






原创粉丝点击