您的位置:首页 > 其它

uva 270 Lining Up(暴力+ 几何)

2013-08-24 13:55 267 查看
题目连接:270 - Lining Up

题目大意:给出一系列点,要求找出共线的点数最大的值。

解题思路:暴力,遍历每两个点确定的直线上又多少点, 每次更新最大值。

三个点共线的性质:A(X1,Y1),B(X2,Y2),C(X3,Y3);这个时候有(Y2-Y1)/(X2-X1) = (Y3-Y2)/(X3-X2)

最好转化成(Y2 - Y1) * (X3 - X2) =  (Y3 - Y2) * (X2 - X1).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int max(const int &a, const int &b) { return a > b ? a : b;}
const int N = 1005;

int n, x
, y
;

int find (int p, int q, int c) {
int cnt = 0;
for (int i = c + 1; i < n; i++) {
if (p * (y[i] - y[c]) == q * (x[i] - x[c]))
cnt++;
}
return cnt;
}

int solve() {
int Max = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int cur = find(x[j] - x[i], y[j] - y[i], j) + 2;
Max = max(Max, cur);
}
}
return Max;
}

int main() {
int cas;
char str
;
scanf("%d%*c%*c", &cas);
while (cas--) {
// Init;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
n = 0;

// Read;
while (gets(str)) {
if (!str[0] && n) break;
sscanf(str, "%d%d", &x
, &y
);
n++;
}

printf("%d\n", solve());
if (cas) printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: