Bicycle Race

来源:互联网 发布:工作软件开发经历 编辑:程序博客网 时间:2024/04/29 18:30

Bicycle Race

Maria participates in a bicycle race.

The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.

Let’s introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).

Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.

Help Maria get ready for the competition — determine the number of dangerous turns on the track.

Input
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track.

The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1).

It is guaranteed that:

the first straight section is directed to the north;
the southernmost (and if there are several, then the most western of among them) point of the track is the first point;
the last point coincides with the first one (i.e., the start position);
any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point);
no pair of points (except for the first and last one) is the same;
no two adjacent straight sections are directed in the same direction or in opposite directions.
Output
Print a single integer — the number of dangerous turns on the track.

题意:一个人在一环形跑道上骑单车,跑道内是一个湖,问有多少个弯会使这个人在不转弯的情况下掉入湖泊

解题思路:直接套一个判断平面内一个点是否在一个图形内的模板就好了

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define N 1002struct point{    double x,y;    point(){}    point(double a,double b)    {        x=a;        y=b;    }}p[N];struct line{    point p1,p2;    int ve;//线段的方向    line(){};    line(point a,point b,int c)    {        p1=a;        p2=b;        ve=c;    }}l[N];int n;int inOrNot(double x1,double y1){    int i, j;    j = n-1;    int res = 0;    for (i = 0; i<n; i++)    {        if((p[i].y<y1 && p[j].y>=y1 || p[j].y<y1 && p[i].y>=y1) && (p[i].x<=x1 || p[j].x<=x1))        {            res ^= ((p[i].x + (y1-p[i].y)/(p[j].y-p[i].y)*(p[j].x-p[i].x)) < x1);        }        j=i;    }  return res;}int main(){    while(~scanf("%d",&n))    {        double a,b;        for(int i=0;i<=n;i++)        {            scanf("%lf %lf",&a,&b);            p[i]=point(a,b);            if(i==1)                l[i-1]=line(p[0],p[1],4);            else if(i>1)            {                double t1,t2;                int ve;                t1=p[i].x-p[i-1].x;                t2=p[i].y-p[i-1].y;                if(t1==0&&t2>0)                    ve=4;                else if(t1==0&&t2<0)                    ve=2;                else if(t1>0&&t2==0)                    ve=1;                else if(t1<0&&t2==0)                    ve=3;                l[i-1]=line(p[i-1],p[i],ve);            }        }        int ans=0;        double x1,y1;        for(int i=0;i<n-1;i++)        {            if(l[i].ve==1)            {                x1=l[i].p2.x+0.5;                y1=l[i].p2.y;                if(inOrNot(x1,y1)==1)                    ans++;            }            if(l[i].ve==2)            {                x1=l[i].p2.x;                y1=l[i].p2.y-0.5;                if(inOrNot(x1,y1)==1)                    ans++;            }            if(l[i].ve==3)            {                x1=l[i].p2.x-0.5;                y1=l[i].p2.y;                if(inOrNot(x1,y1)==1)                    ans++;            }            if(l[i].ve==4)            {                x1=l[i].p2.x;                y1=l[i].p2.y+0.5;                if(inOrNot(x1,y1)==1)                    ans++;            }        }        printf("%d\n",ans);    }}
0 0