Time limit: 1000ms single point time limit: 1000ms memory limit: 256MB
Description
The 4 segments on the plane are given to determine whether the 4 segments are just around a rectangle with an area greater than 0. input
Enter the first line is an integer T (1<=t<=100) that represents the number of test data.
Each group of data contains 4 rows, each containing 4 integers x1, y1, x2, y2 (0 <= x1, y1, x2, y2 <= 100000), where (x1, y1), (X2,y2) represents the two endpoints of a line segment. Output
Each set of data outputs a yes or no to indicate whether the 4 segments entered are exactly rectangular. Sample input
3
0 0 0 1 1 0 1 1 0 1 1 1 1 0 0 0 0 1 2 3 1 0 3 2 3 2 2 3 1 0 0 1 0 1 1 0
0
2 0 1 1 1 1 0 1
Sample output
Yes
Yes
NO
Ideas for solving problems
1: First the rectangle must be 4 points. Read the 2 points of 4 segments sequentially, enter Map,key set to "X,y", and repeat points will overwrite the dots. So the last 4 points left to continue to judge, otherwise output no;
2: Next determine whether the four segments are parallel or vertical. Only need to take the edge L1 in turn and the other three edges l2,l3,l4 judgment on it.
But there are a lot of special cases, there are 4 points, 4 edges are parallel or vertical, but it doesn't necessarily make a rectangle, as shown in the following figure. I give the method is, if parallel, then can not have coincident point, parallel side number Parallelnum plus one, if vertical, then only one coincidence point, overlap the number of verticalnum plus one.
Suddenly remembered I forgot to consider "gives a side of two points of the same", this judgment is also simple, handle it yourself
Code:
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Scanner; public class Main {arraylist<line> linesarray.//public int[] result;
The result of the judgement is public void entry () {Scanner sc = new Scanner (system.in);
int number = Sc.nextint (); Int[][][] lines = new Int[number][4][4];
Line coordinates result = new Int[number]; input for (int i=0; i<number; i++) {for (int j=0; j<4; J + +) {for (int k=0; k<4; k++) {lines[i
][j][k] = Sc.nextint ();
}}//judge for (int i=0; i<number; i++) {//interpretation is 4 dots if (Pointsjudge (i, 4, lines[i]) = = False) {
Continue
//Judge line segments parallel or vertical linesjudge (i, Linesarray);
//output for (int i=0; i<number; i++) {if (result[i] = = 1) {System.out.println ("YES");
} else{System.out.println ("NO"); }}//Determine if it is 4 Points public boolean pointsjudge (int rectanglenumber, int linenumber, int[][] lines) {Linesar Ray = new arraylist<line> (); //Segment Array hashmap<string, point> pointsmap = new hashmap<string, point> ();
Point map for (int i=0; i<linenumber; i++) {Line line = new Line (lines[i));
Linesarray.add (line); Point point1 = Line.getpoint1 ();
P1 String key1 = Point1.getkey ();
Pointsmap.put (Key1, point1); Point Point2 = Line.getpoint2 ();
P2 String key2 = Point2.getkey ();
Pointsmap.put (Key2, Point2);
} if (Pointsmap.size ()!= 4) {//Not 4 dots result[rectanglenumber] = 0;
return false;
return true; }//Line segment parallel, vertical judge public int Linesjudge (int rectanglenumber, arraylist<line> linesarray) {int x1 = Linesarray.get (
0). x1;
int y1 = linesarray.get (0). y1;
int x2 = linesarray.get (0). X2;
int y2 = linesarray.get (0). y2; int verticalnum = 0; Number of vertical segments int parallelnum = 0;
The number of parallel segments for (int i=1; i<linesarray.size (); i++) {int x3 = linesarray.get (i). X1;
int y3 = Linesarray.get (i). Y1;
int x4 = Linesarray.get (i). X2; int y4 = LINESARRAY.GET (i). Y2; Judge Vertical if ((y1-y2) * (y3-y4) = = -1* (x1-x2) * (x3-x4)) {//vertical, and have a dot repeat if ((x1==x3) && (y1==y3) | | (x1==x4) && (y1==y4) | | (x2==x3) && (y2==y3) | |
(x2==x4) && (y2==y4)) {verticalnum++;
Continue
}else{Result[rectanglenumber] = 0;
return 0; }//Judge parallel if ((y1-y2) * (x3-x4) = = (y3-y4) * (X1-X2)) {//parallel, not a bit repetitive if (x1==x3) && (y1==y3) | | X1==X4) && (y1==y4) | | (x2==x3) && (y2==y3) | |
(x2==x4) && (y2==y4)) {Result[rectanglenumber] = 0;
return 0;
}else{parallelnum++;
Continue
}//Neither parallel nor perpendicular result[rectanglenumber] = 0;
} if (verticalnum==2 && parallelnum==1) {//two vertical one parallel result[rectanglenumber] = 1;
return 1;
public static void Main (string[] args) {main m = new main ();
M.entry ();
}//class line{int x1, y1, x2, y2;
Line (int[] line) {this.x1 = line[0];
This.y1 = line[1]; This.x2 = LinE[2];
This.y2 = line[3];
Point GetPoint1 () {Return to New Point (this.x1, this.y1);
Point GetPoint2 () {Return to New Point (this.x2, this.y2);
String Getkey () {return this.x1+ "," +this.y1+ "," +this.x2+ "," +THIS.Y2;
}//class point{int x, y;
Point (int x, int y) {this.x = x;
This.y = y;
int GetX () {return this.x;
int GetY () {return this.y;
String Getkey () {return this.x+ "," +THIS.Y;
}
}
}