LA_3263_That_Nice_Euler_Circuits_(欧拉定理+计算几何基础)

描述


https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1264

给出一个一笔画的所有折点,求这个一笔画共把平面分成了几个区域(包括优先区域与无限区域).

 

3263 - That Nice Euler Circuit

3263
That Nice Euler Circuit
Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his
primary school Joey heard about the nice story of how Euler started the study about graphs. The
problem in that story was – let me remind you – to draw a graph on a paper without lifting your
pen, and finally return to the original position. Euler proved that you could do this if and only if the
(planar) graph you created has the following two properties: (1) The graph is connected; and (2) Every
vertex in the graph has even degree.
Joey’s Euler machine works exactly like this. The device consists of a pencil touching the paper,
and a control center issuing a sequence of instructions. The paper can be viewed as the infinite two-
dimensional plane; that means you do not need to worry about if the pencil will ever go off the boundary.
In the beginning, the Euler machine will issue an instruction of the form (X0, Y 0) which moves the
pencil to some starting position (X0, Y 0). Each subsequent instruction is also of the form (X ′ , Y ′ ),
which means to move the pencil from the previous position to the new position (X ′ , Y ′ ), thus draw a
line segment on the paper. You can be sure that the new position is different from the previous position
for each instruction. At last, the Euler machine will always issue an instruction that move the pencil
back to the starting position (X0, Y 0). In addition, the Euler machine will definitely not draw any
lines that overlay other lines already drawn. However, the lines may intersect.
After all the instructions are issued, there will be a nice picture on Joey’s paper. You see, since the
pencil is never lifted from the paper, the picture can be viewed as an Euler circuit.
Your job is to count how many pieces (connected areas) are created on the paper by those lines
drawn by Euler.
Input
There are no more than 25 test cases. Ease case starts with a line containing an integer N ≥ 4, which
is the number of instructions in the test case. The following N pairs of integers give the instructions
and appear on a single line separated by single spaces. The first pair is the first instruction that gives
the coordinates of the starting position. You may assume there are no more than 300 instructions in
each test case, and all the integer coordinates are in the range (-300, 300). The input is terminated
when N is 0.
Output
For each test case there will be one output line in the format
Case x: There are w pieces.,
where x is the serial number starting from 1.
Note: The figures below illustrate the two sample input cases.
Sample Input
5
0 0 0 1 1 1 1 0 0 0
7
1 1 1 5 2 1 2 5 5 1 3 5 1 1
0ACM-ICPC Live Archive: 3263 – That Nice Euler Circuit
Sample Output
Case 1: There are 2 pieces.
Case 2: There are 5 pieces.
2/2

分析


欧拉定理:平面图的顶点数V,边数E,面数F满足V+F-E=2.

这样一来,只要求点数和边数即可.

点分为两部分:1.本来就有的.2.相交(规范相交)得到的.

边也可以分为两部分:1.本来就有的.2.每因为规范相交而产生一个新的点,边的个数+1.

 

注意:

1.有可能三线共点,此时交点被算了两次,所以要去重,可以用unique函数(数组必须有序,如果从a[1]开始,则返回值要-(a+1)).

2.输入是n+1个点!他在最后把起点又输入了一遍!查了这么久...

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=300+10;
 5 const double eps=1e-8;
 6 
 7 int n,kase;
 8 struct Point{
 9     double x,y;
10     Point(double x=0,double y=0):x(x),y(y){}
11 }p[maxn],v[maxn*maxn];
12 typedef Point Vector;
13 int dcmp(double x){
14     if(fabs(x)<eps) return 0;
15     return x<0?-1:1;
16 }
17 Vector operator + (Vector a,Vector b){ return Vector(a.x+b.x,a.y+b.y); }
18 Vector operator - (Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
19 Vector operator * (Vector a,double p){ return Vector(a.x*p,a.y*p); }
20 Vector operator / (Vector a,double p){ return Vector(a.x/p,a.y/p); }
21 bool operator < (const Point &a,const Point &b){ return a.x<b.x||(a.x==b.x&&a.y<b.y); }
22 bool operator == (const Point &a,const Point &b){ return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; }
23 double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; }
24 double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; }
25 bool segment_proper_intersection(Point a,Point b,Point c,Point d){
26     double c1=cross(b-a,c-a), c2=cross(b-a,d-a),
27            c3=cross(d-c,a-c), c4=cross(d-c,b-c);
28         return (dcmp(c1)^dcmp(c2))==-2&&(dcmp(c3)^dcmp(c4))==-2;
29 }
30 Point get_line_intersection(Point P,Vector v,Point Q,Vector w){
31     Vector u=P-Q;
32     double t=cross(w,u)/cross(v,w);
33     return P+v*t;
34 }
35 bool on_segment(Point p,Point a,Point b){ return dcmp(cross(a-p,b-p))==0&&dcmp(dot(a-p,b-p)<0); }
36 
37 void solve(){
38     for(int i=1;i<=n;i++){
39         scanf("%lf%lf",&p[i].x,&p[i].y);
40         v[i]=p[i];
41     }
42     n--;//输入的是n+1个点
43     int c=n,e=n;
44     for(int i=1;i<=n;i++)
45         for(int j=i+1;j<=n;j++){
46             if(segment_proper_intersection(p[i],p[i+1],p[j],p[j+1]))
47                 v[++c]=get_line_intersection(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]);}
48     sort(v+1,v+c+1);
49     c=unique(v+1,v+c+1)-(v+1);//去重,注意减去的时(v+1)
50     for(int i=1;i<=c;i++)
51         for(int j=1;j<=n;j++)
52             if(on_segment(v[i],p[j],p[j+1])) e++;
53     printf("Case %d: There are %d pieces.\n",++kase,e+2-c);//欧拉定理
54 }
55 int main(){
56     while(scanf("%d",&n)&&n) solve();
57     return 0;
58 }
View Code

 

转载于:https://www.cnblogs.com/Sunnie69/p/5524434.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解一下拉格朗日方程的推导过程,以一维情况为例。 假设一个质点在一维坐标系上运动,其位置用 $q$ 表示,时间用 $t$ 表示。该质点的运动可以用拉格朗日函数 $L(q, \dot{q}, t)$ 描述,其中 $\dot{q}=\frac{dq}{dt}$ 表示质点的速度。 为了推导欧拉-拉格朗日方程,我们首先需要定义一个重要的量,即作用量 $S$。作用量定义为 $$S = \int_{t_1}^{t_2} L(q, \dot{q}, t) dt$$ 其中 $t_1$ 和 $t_2$ 表示质点运动的起点和终点。作用量可以理解为一个积分,它是拉格朗日函数在时间 $t_1$ 到 $t_2$ 内的时间积分。 接下来,我们需要考虑如何对作用量进行变分。变分是一种数学操作,它类似于求导,但是是对函数进行微小的偏移,即对函数进行微小的扰动。对于一个作用量 $S$,其变分可以表示为 $$\delta S = S[q+\delta q] - S[q]$$ 其中 $\delta q$ 表示对 $q$ 进行微小的扰动。 现在我们来考虑如何对作用量进行变分。首先,我们将作用量中的积分拆开,得到 $$\delta S = \int_{t_1}^{t_2} \left( \frac{\partial L}{\partial q} \delta q + \frac{\partial L}{\partial \dot{q}} \delta \dot{q} \right) dt$$ 其中第一个式子中的 $\frac{\partial L}{\partial q}$ 表示对 $L$ 关于 $q$ 的偏导数,第二个式子中的 $\frac{\partial L}{\partial \dot{q}}$ 表示对 $L$ 关于 $\dot{q}$ 的偏导数。 现在我们需要将 $\delta \dot{q}$ 转化为 $\delta q$。由于 $\dot{q}=\frac{dq}{dt}$,我们可以得到 $$\delta \dot{q} = \frac{d}{dt} \delta q$$ 将上式代入到 $\delta S$ 中,得到 $$\delta S = \int_{t_1}^{t_2} \left( \frac{\partial L}{\partial q} \delta q + \frac{\partial L}{\partial \dot{q}} \frac{d}{dt} \delta q \right) dt$$ 接下来,我们需要对第二个式子进行分部积分,得到 $$\delta S = \int_{t_1}^{t_2} \left( \frac{\partial L}{\partial q} - \frac{d}{dt} \frac{\partial L}{\partial \dot{q}} \right) \delta q dt + \left[ \frac{\partial L}{\partial \dot{q}} \delta q \right]_{t_1}^{t_2}$$ 现在我们需要对第二个式子进行讨论。由于质点在运动时,其位置 $q$ 和速度 $\dot{q}$ 在起点和终点上都是确定的,因此 $\delta q$ 在 $t_1$ 和 $t_2$ 处的值都应该为 0。因此,第二个式子等于 0。 最终,我们得到了欧拉-拉格朗日方程: $$\frac{d}{dt} \frac{\partial L}{\partial \dot{q}} - \frac{\partial L}{\partial q} = 0$$ 这个方程描述了质点的运动。如果我们能够求出拉格朗日函数 $L$,那么欧拉-拉格朗日方程就可以帮助我们计算质点的运动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值