参考博客必发娱乐官网

HDOJ 4454 Stealing a Cake 计算几何

 

暴力枚举角度.....

 

There is a big round cake on the ground. A small ant plans to steal a small piece of cake. He starts from a certain point, reaches the cake, and then carry the piece back home. He does not want to be detected, so he is going to design a shortest path to achieve his goal. 

                                               Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5932    Accepted Submission(s): 2599

Problem Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

 

 

Input

The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.

 

 

Output

For each test case, your program should output one section. The first line of each section must be “Test case #参考博客必发娱乐官网。k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.

 

 

Sample Input

2

10 10 20 20

15 15 25 25.5

0

 

 

Sample Output

Test case #1 Total explored area: 180.00

 

必发娱乐官网 1必发娱乐官网 2

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 using namespace std;
 5 typedef struct node
 6 {
 7     double x,y1,y2;
 8     int flag;
 9 } node;
10 typedef struct tree
11 {
12     double y1,y2,x;
13     double cover;
14     bool flag;
15 } treee;
16 node n[400];
17 treee tree[10000000];
18 double q[400];
19 bool cmp(node a,node b)
20 {
21     return a.x<b.x;
22 }
23 void build(int l,int r,int t)
24 {
25     tree[t].cover=0;
26     tree[t].flag=false;
27     tree[t].x=-1;
28     tree[t].y1=q[l];
29     tree[t].y2=q[r];
30     if(l+1==r)
31     {
32         tree[t].flag=true;
33         return ;
34     }
35     int m=(l+r)>>1;
36     build(l,m,t<<1);
37     build(m,r,t<<1|1);
38 }
39 double insert(double x,double y1,double y2,int t,int cover)
40 {
41     if(tree[t].y1>=y2||tree[t].y2<=y1)
42     {
43         return 0;
44     }
45     if(tree[t].flag)
46     {
47         if(tree[t].cover>0)
48         {
49             double sum=0,x1=tree[t].x;
50             sum=(x-x1)*(tree[t].y2-tree[t].y1);
51             tree[t].cover+=cover;
52             tree[t].x=x;
53             return sum;
54         }
55         else
56         {
57             tree[t].cover+=cover;
58             tree[t].x=x;
59             return 0;
60         }
61     }
62     return insert(x,y1,y2,t<<1,cover)+insert(x,y1,y2,t<<1|1,cover);
63 }
64 int main()
65 {
66     double x,y,xx,yy;
67     int m,t,i,ww=1;
68     while(~scanf("%d",&m)&&m)
69     {
70         t=1;
71         for(i=0; i<m; i++)
72         {
73             scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
74             q[t]=y;
75             n[t].flag=1;
76             n[t].x=x;
77             n[t].y1=y;
78             n[t++].y2=yy;
79             q[t]=yy;
80             n[t].flag=-1;
81             n[t].x=xx;
82             n[t].y1=y;
83             n[t++].y2=yy;
84         }
85         sort(n+1,n+t,cmp);
86         sort(q+1,q+t);
87         build(1,t-1,1);
88         double sum=0;
89         for(i=1; i<t; i++)
90         {
91             sum+=insert(n[i].x,n[i].y1,n[i].y2,1,n[i].flag);
92         }
93         cout<<"Test case #"<<ww++<<endl;
94         cout<<"Total explored area: ";
95         printf("%.2lfnn",sum);
96     }
97 }

View Code

 

 

 

 

Stealing a Cake

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2495 Accepted Submission(s): 681

Problem Description There is a big round cake on the ground. A small ant plans to steal a small piece of cake. He starts from a certain point, reaches the cake, and then carry the piece back home. He does not want to be detected, so he is going to design a shortest path to achieve his goal.

The big cake can be considered as a circle on a 2D plane. The ant’s home can be considered as a rectangle. The ant can walk through the cake. Please find out the shortest path for the poor ant.
Input The input consists of several test cases.
The first line of each test case contains x,y, representing the coordinate of the starting point. The second line contains x, y, r. The center of the cake is point (x,y) and the radius of the cake is r. The third line contains x1,y1,x2,y2, representing the coordinates of two opposite vertices of the rectangle --- the ant's home.
All numbers in the input are real numbers range from -10000 to 10000. It is guaranteed that the cake and the ant's home don't overlap or contact, and the ant's starting point also is not inside the cake or his home, and doesn't contact with the cake or his home.
If the ant touches any part of home, then he is at home.
Input ends with a line of 0 0. There may be a blank line between two test cases.
Output For each test case, print the shortest distance to achieve his goal. Please round the result to 2 digits after decimal point.
Sample Input

1 1
-1 1 1
0 -1 1 0
0 2
-1 1 1
0 -1 1 0
0 0

Sample Output

1.75
2.00

Source 2012 Asia Hangzhou Regional Contest

 

 

 

#include 
#include 
#include 
#include 
#include 

using namespace std;

const double eps=1e-6;

int dcmp(double x)
{
    if(fabs(x)0) return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}

const double pi=acos(-1.0);

struct Circle
{
    Point c;
    double r;
    Circle(Point _c=0,double _r=0):c(_c),r(_r){}
    Point point(double a)
    {
        return Point(c.x+cos(a)*r,c.y+sin(a)*r);
    }
};

double a,b,c,d;

Point P1;
Point L[4];
Circle C;

int main()
{
    while(scanf(%lf%lf,&a,&b)!=EOF)
    {
        if(dcmp(a)==0&&dcmp(b)==0) break;

        P1=Point(a,b);
        scanf(%lf%lf%lf,&a,&b,&c);
        C=Circle(Point(a,b),c);
        scanf(%lf%lf%lf%lf,&a,&b,&c,&d);
        if(a>c) swap(a,c);
        if(b>d) swap(b,d);
        L[0]=Point(a,b); L[1]=Point(a,d);
        L[2]=Point(c,d); L[3]=Point(c,b);

        double delta=2.0*pi*0.0001;
        double ans=1e30;

        for(int i=0;i<10000;i++)
        {
   double du=delta*i;
            Point p = Point(C.c.x+cos(du)*C.r,C.c.y+sin(du)*C.r);

            double Part1=DistanceToSegment(p,L[3],L[0]);
   for(int i=0;i<3;i++)
   {
    double temp = DistanceToSegment(p,L[i],L[i+1]);
    Part1=min(Part1,temp);
   }
   double Part2=Length(p-P1);

            ans=min(ans,Part1+Part2);
        }
        printf(%.2lf
,ans);
    }
    return 0;
}

 

 

4454 Stealing a Cake 总结几何 暴力枚举角度..... Stealing a Cake Time Limit: 5000/二零零三 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s):...

The big cake can be considered as a circle on a 2D plane. The ant’s home can be considered as a rectangle. The ant can walk through the cake. Please find out the shortest path for the poor ant.

InputThe input consists of several test cases. 
The first line of each test case contains x,y, representing the coordinate of the starting point. The second line contains x, y, r. The center of the cake is point (x,y) and the radius of the cake is r. The third line contains x1,y1,x2,y2, representing the coordinates of two opposite vertices of the rectangle --- the ant's home. 
All numbers in the input are real numbers range from -10000 to 10000. It is guaranteed that the cake and the ant's home don't overlap or contact, and the ant's starting point also is not inside the cake or his home, and doesn't contact with the cake or his home. 
If the ant touches any part of home, then he is at home. 
Input ends with a line of 0 0. There may be a blank line between two test cases.OutputFor each test case, print the shortest distance to achieve his goal. Please round the result to 2 digits after decimal point.Sample Input

1 1
-1 1 1
0 -1 1 0
0 2
-1 1 1
0 -1 1 0
0 0

Sample Output

1.75
2.00

这是道好题啊。
题意:给定一点,一圆,一矩形(三者互不包含),求从点到圆再到矩形的最短路径。
解题思路:先求出点到矩形最近的点,再求圆到这条直线最近的那个点,将这三点连到一块就是答案。
两种解法,一种是直接枚举圆周上的点,二是三分。

枚举圆周ac代码:

必发娱乐官网 3必发娱乐官网 4

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <string>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <iomanip>
 8 using namespace std;
 9 typedef long long ll;
10 const double inf = 111111;
11 const double pi=acos(-1.0);
12 const double eps = 1e-6;
13 struct nod{
14     double x;
15     double y;
16 }bg,rec[5],pt,q,rel,rer;
17 double r;
18 double getd(nod a,nod b)
19 {
20     double dis=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
21     return dis;
22 }
23 double getdis(nod p,nod a,nod b)//因为点不会在矩形内,所以只需要根据对角线的这两点判断即可。
24 {
25     double xx=0,yy=0;
26     if(p.x<a.x) xx=a.x-p.x;
27     else if(p.x>b.x) xx=p.x-b.x;
28     if(p.y<a.y) yy=a.y-p.y;
29     else if(p.y>b.y) yy=p.y-b.y;
30     return  sqrt(xx*xx+yy*yy);
31 }
32 int main() {
33     ios::sync_with_stdio(false);
34     cout<<setiosflags(ios::fixed)<<setprecision(2);
35     while(cin>>bg.x>>bg.y)
36     {
37         if(fabs(bg.x)<eps&& fabs(bg.y)<eps) break;
38         cin>>pt.x>>pt.y>>r;
39         for(int i=0;i<2;++i)
40         {
41             cin>>rec[i].x>>rec[i].y;
42         }
43         rel.x=min(rec[0].x,rec[1].x);//把点换为(x,y)最小的与最大的两个点。
44         rel.y=min(rec[0].y,rec[1].y);
45         rer.x=max(rec[0].x,rec[1].x);
46         rer.y=max(rec[0].y,rec[1].y);
47         double ans=inf*1.0;
48         for(double dre=0;dre<360.0;dre+=0.01)//枚举圆周上的点
49         {
50             q.x=pt.x+cos(dre/180*pi)*r;
51             q.y=pt.y+sin(dre/180*pi)*r;
52             ans=min(ans,getdis(q,rel,rer)+getd(q,bg));
53         }
54         cout<<setprecision(2)<<ans<<endl;
55     }
56     return 0;
57 }

View Code

 

参照博客:

本文由必发娱乐官网发布于必发88手机网站,转载请注明出处:参考博客必发娱乐官网

您可能还会对下面的文章感兴趣: