import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; /** * Solution to Four Square. * * @author vanb */ public class foursquare_vanb { /** Input. */ private static Scanner sc; /** Output. */ private static PrintStream ps; /** * The Class Rectangle. * * @author vanb */ private class Rectangle { /** The height. */ public int h; /** The width. */ public int w; /** * Instantiates a new rectangle. * * @param h the height * @param w the width */ public Rectangle( int h, int w ) { this.h = h; this.w = w; } /** * To string. * * @return the string */ @Override public String toString() { return "Rectangle [h=" + h + ", w=" + w + "]"; } /** * Swap height and width. */ public void swap() { int t = h; h = w; w = t; } } /** OK is true if we can find a solution. */ private boolean ok = false; /** * Test condition 1. * * +-+--+ * |A| B| * +-++-+ * |C |D| * +--+-+ * * @param a Rectangle A * @param b Rectangle B * @param c Rectangle C * @param d Rectangle D */ private void test1( Rectangle a, Rectangle b, Rectangle c, Rectangle d ) { boolean cando = true; cando &= (a.w+b.w == a.h+c.h); cando &= (a.w+b.w == b.h+d.h); cando &= (c.w+d.w == b.h+d.h); cando &= (a.h==b.h && c.h==d.h); if( cando ) ok = true; } /** * Test condition 2. * * +------+-+ * | |B| * | +-+ * |A |C| * | +-+ * | |D| * +------+-+ * * @param a Rectangle A * @param b Rectangle B * @param c Rectangle C * @param d Rectangle D */ private void test2( Rectangle a, Rectangle b, Rectangle c, Rectangle d ) { boolean cando = true; cando &= (a.h == b.h+c.h+d.h); cando &= (b.w==c.w && c.w==d.w); cando &= (a.h == a.w+b.w); if( cando ) ok = true; } /** * Test condition 3. * * +------+-+-+ * | |B|C| * | +-+-+ * | A | | * | | D | * | | | * +------+---+ * * @param a Rectangle A * @param b Rectangle B * @param c Rectangle C * @param d Rectangle D */ private void test3( Rectangle a, Rectangle b, Rectangle c, Rectangle d ) { boolean cando = true; cando &= (b.w+c.w == d.w); cando &= (b.h == c.h); cando &= (a.h == a.w+d.w); cando &= (a.h == c.h+d.h); if( cando ) ok = true; } /** * Test condition 4. * * +-+-+-+-+ * | | | | | * |A|B|C|D| * | | | | | * +-+-+-+-+ * * @param a Rectangle A * @param b Rectangle B * @param c Rectangle C * @param d Rectangle D */ private void test4( Rectangle a, Rectangle b, Rectangle c, Rectangle d ) { boolean cando = true; cando &= (a.h==b.h && b.h==c.h && c.h==d.h); cando &= (a.w==b.w && b.w==c.w && c.w==d.w); cando &= (a.h == a.w*4); if( cando ) ok = true; } /** * Test condition 5. * * +-+-+---+ * | | | C | * |A|B+---+ * | | | D | * +-+-+---+ * * @param a Rectangle A * @param b Rectangle B * @param c Rectangle C * @param d Rectangle D */ private void test5( Rectangle a, Rectangle b, Rectangle c, Rectangle d ) { boolean cando = true; cando &= (a.h==b.h && b.h==c.h+d.h); cando &= (a.w+b.w+c.w == a.h); cando &= (c.w==d.w); if( cando ) ok = true; } /** * Test all 5 conditions of the given permutation of the Rectangles. * * @param p the permutation */ private void testall( Rectangle p[] ) { test1( p[0], p[1], p[2], p[3] ); test2( p[0], p[1], p[2], p[3] ); test3( p[0], p[1], p[2], p[3] ); test4( p[0], p[1], p[2], p[3] ); test5( p[0], p[1], p[2], p[3] ); } /** The rectangles. */ Rectangle rectangles[] = new Rectangle[4]; /** The permutation. */ Rectangle permutation[] = new Rectangle[4]; /** Which rectangles have already been used in building a permutation. */ boolean used[] = new boolean[4]; /** * Perform all Swaps on a permutation. * * @param level the level */ private void swaps( int level ) { --level; if( level>0 ) swaps( level ); permutation[level].swap(); testall( permutation ); if( level>0 ) swaps( level ); } /** * Permute the Rectangles. * * @param level the level */ private void permute( int level ) { if( level==0 ) { testall( permutation ); swaps( 4 ); } else { --level; for( int i=0; i<4; i++ ) if( !used[i] ) { permutation[level] = rectangles[i]; used[i] = true; permute( level ); used[i] = false; } } } /** * Do it!. */ private void doit() { rectangles[0] = new Rectangle( sc.nextInt(), sc.nextInt() ); rectangles[1] = new Rectangle( sc.nextInt(), sc.nextInt() ); rectangles[2] = new Rectangle( sc.nextInt(), sc.nextInt() ); rectangles[3] = new Rectangle( sc.nextInt(), sc.nextInt() ); Arrays.fill( used, false ); permute( 4 ); ps.println( ok ? 1 : 0 ); } /** * Main. * * @param args unused * @throws Exception the exception */ public static void main( String[] args ) throws Exception { sc = new Scanner( System.in ); ps = System.out; new foursquare_vanb().doit(); } }