PROBLEM STATEMENT
The Chef is given a list of N triangles. Each triangle is identfied by the coordinates of its three corners in the 2-D cartesian plane. His job is to figure out how many
of the given triangles are right triangles. A right triangle is a triangle in which one angle is a 90 degree angle. The vertices
of the triangles have integer coordinates and all the triangles given are valid( three points aren't colinear ).
of the given triangles are right triangles. A right triangle is a triangle in which one angle is a 90 degree angle. The vertices
of the triangles have integer coordinates and all the triangles given are valid( three points aren't colinear ).
Input
The first line of the input contains an integer N denoting the number of triangles. Each of the following N
lines contain six space separated integers x1 y1 x2 y2 x3 y3 where (x1, y1),
(x2, y2) and (x3, y3) are the vertices of a triangle.
lines contain six space separated integers x1 y1 x2 y2 x3 y3 where (x1, y1),
(x2, y2) and (x3, y3) are the vertices of a triangle.
Output
Output one integer, the number of right triangles among the given triangles.
Constraints
- 1 ≤ N ≤ 100000 (105)
- 0 ≤ x1, y1, x2, y2, x3, y3 ≤ 20
Example
Input: 5 0 5 19 5 0 0 17 19 12 16 19 0 5 14 6 13 8 7 0 4 0 14 3 14 0 2 0 14 9 2 Output: 3
PROBLEM LINK:
RIGHTRI
The idea : The idea is to use pythagoras theorem c^2 = a^2+ b^2 or 2*c^2 = a^2+b^2+c^2 .
We can do this by creating a class point and writing a function to calculate difference.
The Code:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct Point
{
int x,y;
Point (int x,int y)
{
this->x=x;
this->y=y;
}
double diff(Point &p)
{
double ans= (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);
return ans;
}
};
int main()
{
int T;
scanf("%d",&T);
int count=0;
while(T--)
{
int x1,y1,x2,y2,x3,y3;
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
Point p1(x1,y1);
Point p2(x2,y2);
Point p3(x3,y3);
double side1=p1.diff(p2);
//cout<<side1<<endl;
double side2=p2.diff(p3);
//cout<<side2<<endl;
double side3=p1.diff(p3);
double hyp=max(side1,side2);
hyp=max(hyp,side3);
if(2*hyp== side1+side2+side3)
count++;
}
cout<<count<<endl;
return 0;
}
No comments:
Post a Comment