Game of Lines(POJ-3668)
Problem Description
Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i has the integer coordinates Xi and Yi (-1,000 ≤ Xi ≤ 1,000; -1,000 ≤ Yi ≤ 1,000).
Bessie can score a point in the game by picking two of the dots and drawing a straight line between them; however, she is not allowed to draw a line if she has already drawn another line that is parallel to that line. Bessie would like to know her chances of winning, so she has asked you to help find the maximum score she can obtain.
Input
Line 1: A single integer: N
Lines 2..N+1: Line i+1 describes lattice point i with two space-separated integers: Xi and Yi.
Output
Line 1: A single integer representing the maximal number of lines Bessie can draw, no two of which are parallel.
Sample Input
4
-1 1
-2 0
0 0
1 1
Sample Output
4
題意:給出n個點的坐標,求最多能確定多少個互不平行的直線。
思路:用atan()函數求出所有點的斜率,排序后進行判斷即可。
Source Program
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 1050 #define MOD 123 #define E 1e-6 using namespace std; struct Point{double x;double y; }point[N]; double a[N*N]; double Calculate(Point p1, Point p2) {Point p;p.x=p2.x-p1.x;p.y=p2.y-p1.y;if(fabs(p.x)<E)//斜率不存在return 1;double temp=atan(p.y/p.x);if(temp<0)//注意斜率的范圍return 1+temp;elsereturn temp; }int main() {int n;scanf("%d",&n);for(int i=0;i<n;i++)scanf("%lf%lf",&point[i].x,&point[i].y);int k=0;for(int i=0;i<n;i++)for(int j=i+1;j<n;j++){a[k]=Calculate(point[i],point[j]);k++;}sort(a,a+k);int ans=1;for(int i=1;i<k;i++)if(a[i]!=a[i-1])ans++;printf("%d\n",ans);return 0; }?
總結
以上是生活随笔為你收集整理的Game of Lines(POJ-3668)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fence Repair(POJ-325
- 下一篇: 基础算法 —— 递推算法