uva1342 That Nice Euler Circuit

137 篇文章 0 订阅

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 nally 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 in nite 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 ( X 0 ;Y 0) which moves the pencil to
some starting position ( X 0 ;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 ( X 0 ;Y 0). In addition, the
Euler machine will de nitely 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 rst pair is the rst 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 gures below illustrate the two sample
input cases.

根据欧拉定理 V+FE=2 ,只需要算出点数 V 和边数E就可以知道面数 F <script type="math/tex" id="MathJax-Element-4">F</script>。求点数需要两两求交点然后去重,求边数需要判断点在线段上。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double eps=1e-8;
struct vec
{
    double x,y;
    void rd()
    {
        scanf("%lf%lf",&x,&y);
    }
    bool operator < (const vec v1) const
    {
        return (x+eps<v1.x)||(fabs(x-v1.x)<eps&&y+eps<v1.y);
    }
    bool operator == (vec v1)
    {
        vec self=(vec){x,y};
        return !(self<v1)&&!(v1<self);
    }
    vec operator + (const vec &v1) const
    {
        return (vec){x+v1.x,y+v1.y};
    }
    vec operator - (const vec &v1) const
    {
        return (vec){x-v1.x,y-v1.y};
    }
    vec operator * (const double &k) const
    {
        return (vec){x*k,y*k};
    }
    vec operator / (const double &k) const
    {
        return (vec){x/k,y/k};
    }
}a[100010],ver[100010];
typedef vec point;
int n,m;
double dot(vec v1,vec v2)
{
    return v1.x*v2.x+v1.y*v2.y;
}
double cross(vec v1,vec v2)
{
    return v1.x*v2.y-v1.y*v2.x;
}
struct line
{
    point p;
    vec a;
};
point intersection(line l1,line l2)
{
    vec u=l1.p-l2.p;
    double t=cross(l2.a,u)/cross(l1.a,l2.a);
    return l1.p+l1.a*t;
}
struct seg
{
    point a,b;
};
bool intersect(seg s1,seg s2)
{
    double x1=cross(s2.b-s2.a,s1.a-s2.a),x2=cross(s2.b-s2.a,s1.b-s2.a),
    y1=cross(s1.b-s1.a,s2.a-s1.a),y2=cross(s1.b-s1.a,s2.b-s1.a);
    int ok1=int(x1<eps)+int(x2<eps),ok2=int(y1<eps)+int(y2<eps);
    return ok1==1&&ok2==1;
}
bool onseg(point p,seg s)
{
    return fabs(cross(s.b-s.a,p-s.a))<eps&&dot(p-s.a,p-s.b)+eps<0;
}
int solve()
{
    int ans;
    n--;
    for (int i=1;i<=n+1;i++)
        a[i].rd();
    m=n;
    for (int i=1;i<=n;i++) ver[i]=a[i];
    for (int i=1;i<=n;i++)
        for (int j=i+1;j<=n;j++)
            if (intersect((seg){a[i],a[i+1]},(seg){a[j],a[j+1]}))
                ver[++m]=intersection((line){a[i],a[i+1]-a[i]},(line){a[j],a[j+1]-a[j]});
    sort(ver+1,ver+m+1);
    m=unique(ver+1,ver+m+1)-ver-1;
    ans=n-m+2;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            if (onseg(ver[j],(seg){a[i],a[i+1]})) ans++;
    return ans;
}
int main()
{
    int K=0;
    while (scanf("%d",&n)&&n)
        printf("Case %d: There are %d pieces.\n",++K,solve());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值