Tuesday, July 15, 2008

Google Code Jam: Triangle Trilemma

Triangle Trilemma

http://code.google.com/codejam/contest/dashboard?c=agdjb2RlamFtcg4LEghjb250ZXN0cxhRDA

Problem

You're interested in writing a program to classify triangles. Triangles can be classified according to their internal angles. If one of the internal angles is exactly 90 degrees, then that triangle is known as a "right" triangle. If one of the internal angles is greater than 90 degrees, that triangle is known as an "obtuse" triangle. Otherwise, all the internal angles are less than 90 degrees and the triangle is known as an "acute" triangle.

Triangles can also be classified according to the relative lengths of their sides. In a "scalene" triangle, all three sides have different lengths. In an "isosceles" triangle, two of the sides are of equal length. (If all three sides have the same length, the triangle is known as an "equilateral" triangle, but you can ignore this case since there will be no equilateral triangles in the input data.)

Your program must determine, for each set of three points, whether or not those points form a triangle. If the three points are not distinct, or the three points are collinear, then those points do not form a valid triangle. (Another way is to calculate the area of the triangle; valid triangles must have non-zero area.) Otherwise, your program will classify the triangle as one of "acute", "obtuse", or "right", and one of "isosceles" or "scalene".

Input

The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as

x1 y1 x2 y2 x3 y3

Output

For each test case, output one line containing "Case #x: " followed by one of these strings:

  • isosceles acute triangle
  • isosceles obtuse triangle
  • isosceles right triangle
  • scalene acute triangle
  • scalene obtuse triangle
  • scalene right triangle
  • not a triangle

Limits

1 ≤ N ≤ 100,
x1, y1, x2, y2, x3, y3 will be integers.

Small dataset

0 ≤ x1, y1, x2, y2, x3, y3 ≤ 9

Large dataset

-1000 ≤ x1, y1, x2, y2, x3, y3 ≤ 1000

Sample


Input





Output
8
0 0 0 4 1 2
1 1 1 4 3 2
2 2 2 4 4 3
3 3 3 4 5 3
4 4 4 5 5 6
5 5 5 6 6 5
6 6 6 7 6 8
7 7 7 7 7 7





Case #1: isosceles obtuse triangle
Case #2: scalene acute triangle
Case #3: isosceles acute triangle
Case #4: scalene right triangle
Case #5: scalene obtuse triangle
Case #6: isosceles right triangle
Case #7: not a triangle
Case #8: not a triangle







My Solution:


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Triangle {

public String classify(String input){
String[] spl = input.split(" ");
long x1 = new Integer(spl[0]);
long y1 = new Integer(spl[1]);
long x2 = new Integer(spl[2]);
long y2 = new Integer(spl[3]);
long x3 = new Integer(spl[4]);
long y3 = new Integer(spl[5]);

StringBuffer sb = new StringBuffer();

if ((y2-y1)*(x3-x1)==(y3-y1)*(x2-x1)) return "not a triangle";

long s12= (long)(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2));

long s23= (long)(Math.pow(x3-x2, 2)+Math.pow(y3-y2, 2));

long s13= (long)(Math.pow(x3-x1, 2)+Math.pow(y3-y1, 2));
if (s12==0||s23==0||s12==0) return "not a triangle";


if (s12==s23||s23==s13||s12==s13)
sb.append("isosceles ");
else
sb.append("scalene ");

long min = Math.min(Math.min(s12, s13),s23);

long max = Math.max(Math.max(s12, s13),s23);

long mid = s12+s23+s13-min-max;

if (min+mid==max) sb.append("right ");

if (min+mid<max) sb.append("obtuse ");

if (min+mid>max) sb.append("acute ");

sb.append("triangle");

return sb.toString();
}

public static void main(String[] args) throws FileNotFoundException {

Triangle an = new Triangle();
System.out.println(an.classify("0 0 0 4 1 2"));
System.out.println(an.classify("1 1 1 4 3 2"));
System.out.println(an.classify("2 2 2 4 4 3"));
System.out.println(an.classify("3 3 3 4 5 3"));
System.out.println(an.classify("4 4 4 5 5 6"));
System.out.println(an.classify("5 5 5 6 6 5"));
System.out.println(an.classify("6 6 6 7 6 8"));
System.out.println(an.classify("7 7 7 7 7 7"));
Scanner scan = new Scanner(new File("Input.txt"));
PrintWriter pw = new PrintWriter("Output.txt");
Integer count =new Integer(scan.nextLine());
for (int i=0;i<count;i++){
pw.write("Case #"+(i+1)+": "+an.classify(scan.nextLine())+"\n");
}
pw.close();
scan.close();
}

}

No comments: