Game of Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7330 Accepted: 2752
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
Source
USACO 2008 February Silver
该题比较简单,就是用 双重循环把有多少条不平行的直线就行,开一个二维数组,储存每种平行线的斜率,化简斜率,用二维数组分别表示分子,分母,其中要注意分子分母为0的情况,下面是我AC的代码
**
#include <iostream>
#include <algorithm>
#include <cstdio>
char flag[4050][2050];
struct point{
int x,y;
};
point p[205];
int g(int x,int y)//求最大公约数
{
if(x<y){
int t=x;
x=y;
y=t;
}
int r;
while(y>0)
{
r=x%y;
x=y;
y=r;
}
return x;
}
int main(){
int n;
int countn=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
int x=p[j].x-p[i].x;
int y=p[j].y-p[i].y;
if(x==0){
if(flag[0][1]!='1'){
countn++;
flag[0][1]='1';
}
}
else if(y==0){
if(flag[1][0]!='1'){
countn++;
flag[1][0]='1';}
}
else{
int k;
if(x>0&&y<0){
x=-x;
y=-y;
k=g(-x,y);
}
else if(x<0&&y<0){
x=-x;
y=-y;
k=g(x,y);
}
else if(x<0&&y>0){
k=g(-x,y);
}
else k=g(x,y);
x=x/k+2002;
y=y/k+2;
if(flag[x][y]!='1'){
countn++;
flag[x][y]='1';
}
}
}
}
printf("%d\n",countn);
return 0;
}
**