Algorithm
Little Petya very much likes rectangles and especially squares. Recently he has received 8 points on the plane as a gift from his mother. The points are pairwise distinct. Petya decided to split them into two sets each containing 4 points so that the points from the first set lay at the vertexes of some square and the points from the second set lay at the vertexes of a rectangle. Each point of initial 8 should belong to exactly one set. It is acceptable for a rectangle from the second set was also a square. If there are several partitions, Petya will be satisfied by any of them. Help him find such partition. Note that the rectangle and the square from the partition should have non-zero areas. The sides of the figures do not have to be parallel to the coordinate axes, though it might be the case.
You are given 8 pairs of integers, a pair per line — the coordinates of the points Petya has. The absolute value of all coordinates does not exceed 104. It is guaranteed that no two points coincide.
Print in the first output line "YES" (without the quotes), if the desired partition exists. In the second line output 4 space-separated numbers — point indexes from the input, which lie at the vertexes of the square. The points are numbered starting from 1. The numbers can be printed in any order. In the third line print the indexes of points lying at the vertexes of a rectangle in the similar format. All printed numbers should be pairwise distinct.
If the required partition does not exist, the first line should contain the word "NO" (without the quotes), after which no output is needed.
0 0
10 11
10 0
0 11
1 1
2 2
2 1
1 2
YES
5 6 7 8
1 2 3 4
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
NO
0 0
4 4
4 0
0 4
1 2
2 3
3 2
2 1
YES
1 2 3 4
5 6 7 8
Pay attention to the third example: the figures do not necessarily have to be parallel to the coordinate axes.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <algorithm>
int main(){
const long N = 8;
long x[N], y[N];
for(long p = 0; p < N; p++){scanf("%ld %ld", x + p, y + p);}
long rv[N]; for(long p = 0; p < N; p++){rv[p] = p;}
bool possible(false);
do{
long M = N / 2; long dx[M], dy[M];
for(long p = 0; p < M; p++){dx[p] = x[rv[(p + 1) % M]] - x[rv[p]]; dy[p] = y[rv[(p + 1) % M]] - y[rv[p]];}
if((dx[0]* dy[2] != dy[0] * dx[2]) || (dx[1] * dy[3] != dy[1] * dx[3]) || (dx[0] * dx[1] + dy[0] * dy[1] != 0) || (dx[0] * dx[0] + dy[0] * dy[0] != dx[1] * dx[1] + dy[1] * dy[1])){continue;}
for(long p = M; p < N; p++){dx[p - M] = x[rv[M + (p + 1) % M]] - x[rv[p]]; dy[p - M] = y[rv[M + (p + 1) % M]] - y[rv[p]];}
if((dx[0] * dy[2] != dy[0] * dx[2]) || (dx[1] * dy[3] != dy[1] * dx[3]) || (dx[0] * dx[1] + dy[0] * dy[1] != 0)){continue;}
possible = true; break;
}while(std::next_permutation(rv, rv + N));
if(possible){
puts("YES");
for(long p = 0; p < N / 2; p++){printf("%ld ", 1 + rv[p]);}; puts("");
for(long p = N / 2; p < N; p++){printf("%ld ", 1 + rv[p]);}; puts("");
}
else{puts("NO">;}
return 0;
}
Copy The Code &
Try With Live Editor
Input
10 11
10 0
0 11
1 1
2 2
2 1
1 2
Output
5 6 7 8
1 2 3 4
Demonstration
Codeforces Solution-B. Rectangle and Square-Solution in C, C++, Java, Python