Croatian Open Competition in Informatics, contest 3, December 8, 2007
4. DEJAVU\$N\$ points are placed in the coordinate plane.
Write a program that calculates how many ways we can choose three points so that they form a right triangle with legs parallel to the coordinate axes.
A right triangle has one 90-degree internal angle. The legs of a right triangle are its two shorter sides.
Input
The first line of input contains the integer \$N\$, the number of points, where \$3 \le N \le 100000\$.
Each of the following \$N\$ lines contains two integers \$X\$ and \$Y (1 \le X, Y \le 100000)\$, the coordinates of one point.
No points will share the same pair of coordinates.
Output
Output the number of triangles.
Sample input
3 4 2 2 1 1 3
Sample output
0
This is my solution:
For every point, I checked other point. If two points had matching x coordinates and different y coordinates, I looked through the points to find a point with same y coordinate as the new point and different x. If found, I checked if the right angled hypotenus checks out in the three points.
Similarly, I repeated a modification of this for two points with matching y coordinates and different x.
The program gets the right result, but needs far too long.
#include<iostream>
#include<cmath>
using namespace std;
/*
double distwithoutroot(int x1, int y1, int x2, int y2) {
//cout << "Got here for values " << x1 << y1 << x2 << y2 << endl;
int xdist = pow((x2 - x1),2);
int ydist = pow((y2 - y1),2);
return xdist + ydist;
}
*/
int main() {
int noofpoints;
int conditionnotsatisfied = 0;
cin >> noofpoints;
int xs[100000];
int ys[100000];
int count = 0;
for (int i = 0; i < noofpoints; i++) {
cin >> xs[i] >> ys[i];
}
for (int i = 0; i < noofpoints; i++) {
int main_x_point = xs[i];
int main_y_point = ys[i];
for (int j = 0; j < noofpoints; j++) {
int checkmatchx = xs[j];
int checkmatchy = ys[j];
if (main_x_point == checkmatchx && main_y_point != checkmatchy) {
for (int k = 0; k < noofpoints; k++) {
int secondcheckx = xs[k];
int secondchecky = ys[k];
if (checkmatchy == secondchecky && checkmatchx != secondcheckx) {
// int hypotenus = distwithoutroot(main_x_point, main_y_point, secondcheckx, secondchecky);
//hypotenus = pow(hypotenus,2);
//int perpendicular = distwithoutroot(main_x_point, main_y_point, checkmatchx, checkmatchy);
//perpendicular = pow(perpendicular,2);
//int base = distwithoutroot(secondcheckx, secondchecky, checkmatchx, checkmatchy);
//base = pow(base,2);
//if (hypotenus== ( perpendicular+ base )) {
count += 1;
//cout << main_x_point << " " << main_y_point << " " << checkmatchx << " " << checkmatchy << " " << secondcheckx << " " << secondchecky << endl;
//xs[i] = 0;
//ys[i] = 0;
//}
}
}
}
else if (main_y_point == checkmatchy && main_x_point != checkmatchx) {
for (int k = 0; k < noofpoints; k++) {
int secondcheckx = xs[k];
int secondchecky = ys[k];
if (checkmatchx == secondcheckx && checkmatchy != secondchecky) {
// int hypotenus = distwithoutroot(main_x_point, main_y_point, secondcheckx, secondchecky);
//hypotenus = pow(hypotenus,2);
// int base = distwithoutroot(main_x_point, main_y_point, checkmatchx, checkmatchy);
//base = pow(base,2);
//int perpendicular = distwithoutroot(secondcheckx, secondchecky, checkmatchx, checkmatchy);
//perpendicular = pow(perpendicular,2);
//if (hypotenus == (perpendicular + base)) {
count += 1;
//cout << main_x_point << " " << main_y_point << " " << checkmatchx << " " << checkmatchy << " " << secondcheckx << " " << secondchecky << endl;
//xs[i] = 0;
//ys[i] = 0;
// }
}
}
}
}
}
//cout << "count value after first check " << count << endl;
//cout << "Condition not satisfid " << conditionnotsatisfied << endl;
/*
for (int i = 0; i < noofpoints; i++) {
int main_x_point = xs[i];
int main_y_point = ys[i];
for (int j = 0; j < noofpoints; j++) {
int checkmatchx = xs[j];
int checkmatchy = ys[j];
if (main_y_point == checkmatchy && main_x_point != checkmatchx) {
for (int k = 0; k < noofpoints; k++) {
int secondcheckx = xs[k];
int secondchecky = ys[k];
if (checkmatchx == secondcheckx && checkmatchy != secondchecky) {
int hypotenus = distwithoutroot(main_x_point, main_y_point, secondcheckx, secondchecky);
//hypotenus = pow(hypotenus,2);
int base = distwithoutroot(main_x_point, main_y_point, checkmatchx, checkmatchy);
//base = pow(base,2);
int perpendicular = distwithoutroot(secondcheckx, secondchecky, checkmatchx, checkmatchy);
//perpendicular = pow(perpendicular,2);
if (hypotenus == (perpendicular + base)) {
count += 1;
//cout << main_x_point << " " << main_y_point << " " << checkmatchx << " " << checkmatchy << " " << secondcheckx << " " << secondchecky << endl;
//xs[i] = 0;
//ys[i] = 0;
}
}
}
}
}
}
*/
cout << count/2;
}