import java.util.List; import java.util.ArrayList; interface Position { public List getAdjacents( ); public boolean getValue( ); } class World { private class MyPosition implements Position { public MyPosition( int r, int c ) { row = r; col = c; } public List getAdjacents( ) { /* Redacted */ } public boolean equals( Object other ) { if( ! ( other instanceof MyPosition ) ) return false; MyPosition rhs = (MyPosition) other; return row == rhs.row && col == rhs.col; } public boolean getValue( ) { return water[ row ][ col ]; } public String toString( ) { return "(" + row + "," + col + ")"; } private int row; private int col; } private boolean [ ] [ ] water = { { true, false, true, false, false, false }, { true, false, true, true, false, true }, { false, false, false, true, true, true }, { true, true, true, true, false, false }, { false, true, false, true, false, true }, { false, false, false, true, true, true } }; public Position newPosition( int r, int c ) { return new MyPosition( r, c ); } // Public driver public List getIsland( Position p ) { ArrayList result = new ArrayList( ); getIsland( p, result ); return result; } // Recursive routine private void getIsland( Position pos, List island ) { if( ( !pos.getValue( ) ) && !island.contains( pos ) ) { island.add( pos ); for( Position p : pos.getAdjacents( ) ) getIsland( p, island ); } } } class Day7 { public static void main( String [ ] args ) { World w1 = new World( ); System.out.println( w1.getIsland( w1.newPosition( 0, 1 ) ) ); System.out.println( w1.getIsland( w1.newPosition( 4, 3 ) ) ); System.out.println( w1.getIsland( w1.newPosition( 4, 4 ) ) ); } }