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); }}
- Bicycle Race
- Bicycle Race
- Bicycle Race
- cf - #346 D-Bicycle Race
- codeforces 659 D. Bicycle Race
- Codeforces 659D Bicycle Race【计算几何】
- 【Codeforces659D】Bicycle Race【向量叉积】
- CodeForces 659D Bicycle Race(叉积)
- D - Bicycle Race CodeForces 659D
- Codeforces Round #346 (Div. 2) D. Bicycle Race
- Codeforces Round #346 (Div. 2) D. Bicycle Race
- Codeforces Round #346 (Div. 2) - D Bicycle Race
- CodeForces - 659D Bicycle Race (数学几何&技巧转换)
- Codeforces Round #346 (Div. 2) D. Bicycle Race
- 简单数学题或者计算几何:Codeforces 659D-Bicycle Race
- CodeForces 659 D. Bicycle Race(计算几何 + 数学公式)
- Codeforces Round #346 (Div. 2) D Bicycle Race (叉积)
- Bicycle
- 【1015】德才论 (结构体排序)
- 读书记录
- 继承
- html5的canvas时钟
- 云服务器评测--BAT(阿里云、腾讯云、百度云)云服务器哪家强?
- Bicycle Race
- Codeforces-149D-Coloring Brackets【区间DP】
- Python初学者的17个技巧
- 关于js原型继承的理解
- KMP算法
- 最大流最小割(max flow min cut)
- 日新进用户200W+,解密《龙之谷》手游背后的压测故事
- MySQL引擎介绍ISAM,MyISAM,HEAP,InnoDB
- Android Hook 步骤