Ultraviolet A 270-lining up

Source: Internet
Author: User

Link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 206

Original question:

''How am I ever going to solve this problem? "Said the pilot.

Indeed, the pilot was not facing an easy task. she had to drop packages at specific points scattered in a dangerous area. furthermore, the pilot cocould only fly over the area once in a straight line, and
She had to fly over as your points as possible. all points were given by means of integer coordinates in a two-dimen1_space. the Pilot wanted to know the largest number of points from the given set that all lie on one line. can you write a program that
Calculates this number?

Your program has to be efficient!

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. this line is followed by a blank line, and there is also
A blank line between two consecutive inputs.
The input consistsNPairs of integers, where 1 <N<1, 700. each pair of integers is separated by one blank and ended by a new-line character. the list of pairs is ended with an end-of-file character. no pair will occur twice.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
The output consists of one integer representing the largest number of points that all lie on one line.

Sample Input

11 12 23 39 1010 11

Sample output

3



Question:

Give a series of points, and find the maximum number of points that can be connected into a line.


Analysis and Summary:

If two points determine a straight line, you can enumerate all the two points, and then traverse other points in a straight line based on the two points, several points are determined on this line.

To determine whether p1 (x1, Y1), P2 (X2, Y2), and P3 (X3, Y3) are in a straight line, check whether P1, p2 slope and P2, P3 slope is equal, mathematical formula is (x1-x2)/(y1-y2) = (x2-x3)/(y2-y3), but the direct division compared to the slope of the accuracy will be lost, so using the diagonal line to multiply, you can convert this formula into (x1-x2) * (y2-y3) = (y1-y2) * (x2-x3 ).

Then the brute force enumeration is performed.


Code:

/* * UVa:  270 - Lining Up * Time: 0.888s * Author: D_Double * */#include<cstdio>#include<cmath>#include<algorithm>#define MAXN 705using namespace std;struct Node{    int x,y;}arr[MAXN];int nIndex;char str[1000];inline void input(){    nIndex=0;    while(gets(str)){        if(!str[0])break;        sscanf(str,"%d%d",&arr[nIndex].x,&arr[nIndex].y);        ++nIndex;    }}inline bool is_in_line(int X1,int Y1,int X2,int Y2,int X3,int Y3){    return (X1-X2)*(Y3-Y2)-(X3-X2)*(Y1-Y2)==0;}void solve(){    int maxNum=2;    for(int i=0; i<nIndex; ++i){        for(int j=i+1; j<nIndex; ++j){            int cnt=2;            for(int k=j+1; k<nIndex; ++k){                if(is_in_line(arr[i].x,arr[i].y,arr[j].x,arr[j].y,arr[k].x,arr[k].y))                     ++cnt;            }            if(cnt>maxNum) maxNum=cnt;        }    }    printf("%d\n", maxNum);}int main(){    int T;    scanf("%d%*c",&T);    gets(str);    while(T--){        input();        if(nIndex==1)printf("1\n");        else if(nIndex==2)printf("2\n");        else  solve();        if(T) printf("\n");    }    return 0;}

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.