import java.util.*; /** * Calculate the size of the square that we should be filling then walk * along the perimeter of this square. For each order of rectangles and * for each possible orientation, place each rectangle along the perimeter * at the first available position. After placing each rectangle make * sure that it does not exceed the bounds of the square nor intersect * an already placed rectangle. * * @author Finn Lidbetter */ public class foursq_finn { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] lengths = new int[4]; int[] widths = new int[4]; int area = 0; for (int i=0; i<4; i++) { lengths[i] = sc.nextInt(); widths[i] = sc.nextInt(); area += lengths[i]*widths[i]; } int side = (int)Math.sqrt(area); if (side*side!=area) { System.out.println(0); return; } for (int i=0; i<4; i++) { if (lengths[i]>side || widths[i]>side) { System.out.println(0); return; } } int[][] permutations = { {0,1,2,3}, {0,1,3,2}, {0,2,1,3}, {0,2,3,1}, {0,3,1,2}, {0,3,2,1}, {1,0,2,3}, {1,0,3,2}, {1,2,0,3}, {1,2,3,0}, {1,3,0,2}, {1,3,2,0}, {2,0,1,3}, {2,0,3,1}, {2,1,0,3}, {2,1,3,0}, {2,3,0,1}, {2,3,1,0}, {3,0,1,2}, {3,0,2,1}, {3,1,0,2}, {3,1,2,0}, {3,2,0,1}, {3,2,1,0} }; for (int permutationIndex=0; permutationIndex0) { int tmp = w; w = l; l = tmp; } Rectangle r; if (firstUncovered.loside || r.y2>side) { valid = false; break; } for (int j=0; j covered = new ArrayList<>(); for (Rectangle r: rectangles) { if (r==null) { break; } int x1 = r.x1; int y1 = r.y1; int x2 = r.x2; int y2 = r.y2; if (x1==0) { covered.add(new Interval(4*side - y2 ,4*side - y1)); } if (y1==0) { covered.add(new Interval(x1, x2)); } if (x2==side) { covered.add(new Interval(side + y1, side + y2)); } if (y2==side) { covered.add(new Interval(3*side - x2, 3*side - x1)); } } Collections.sort(covered); if (covered.size()==0) { return new Interval(0, 4*side); } if (covered.size()==1) { if (covered.get(0).lo!=0) { return new Interval(0, covered.get(0).lo); } } for (int i=1; i=r2.x2 || y2<=r2.y1 || y1>=r2.y2) { return false; } return true; } public String toString() { return String.format("[(%d,%d),(%d,%d)]",x1,y1,x2,y2); } } class Interval implements Comparable{ int lo,hi; public Interval(int llo, int hhi) { lo = llo; hi = hhi; } public int compareTo(Interval i2) { if (lo==i2.lo) { return hi - i2.hi; } return lo - i2.lo; } public String toString() { return String.format("[%d,%d]",lo,hi); } }