public class BinPacking { public static int solveBP(int k1, int s1, int k2, int s2, int k3, int s3, int remCap, int Cap) { int sol1, sol2, sol3; int my_sol, my_sol1, my_sol2, my_sol3; sol1 = sol2 = sol3 = Integer.MAX_VALUE; my_sol = my_sol1 = my_sol2 = my_sol3 = Integer.MAX_VALUE; if ( k1 == 0 && k2 == 0 && k3 == 0 ) { /* System.out.println("-->" + k1 + " " + s1 + " " + k2 + " " + s2 + " " + k3 + " " + s3 + " " + remCap + " " + Cap + "*\t===> " + 1 + " box"); */ return(1); } if ( k1 > 0 ) { if ( s1 <= remCap ) { sol1 = solveBP( k1-1, s1, k2, s2, k3, s3, remCap - s1, Cap); my_sol1 = sol1; } else { sol1 = solveBP( k1-1, s1, k2, s2, k3, s3, Cap - s1, Cap); my_sol1 = sol1 + 1; } } if ( k2 > 0 ) { if ( s2 <= remCap ) { sol2 = solveBP( k1, s1, k2-1, s2, k3, s3, remCap - s2, Cap); my_sol2 = sol2; } else { sol2 = solveBP( k1, s1, k2-1, s2, k3, s3, Cap - s2, Cap); my_sol2 = sol2 + 1; } } if ( k3 > 0 ) { if ( s3 <= remCap ) { sol3 = solveBP( k1, s1, k2, s2, k3-1, s3, remCap - s3, Cap); my_sol3 = sol3; } else { sol3 = solveBP( k1, s1, k2, s2, k3-1, s3, Cap - s3, Cap); my_sol3 = sol3 + 1; } } if ( my_sol1 <= my_sol2 && my_sol1 <= my_sol3 ) my_sol = my_sol1; if ( my_sol2 <= my_sol1 && my_sol2 <= my_sol3 ) my_sol = my_sol2; if ( my_sol3 <= my_sol1 && my_sol3 <= my_sol2 ) my_sol = my_sol3; /* System.out.println("-->" + k1 + " " + s1 + " " + k2 + " " + s2 + " " + k3 + " " + s3 + " " + remCap + " " + Cap + " \t===> " + my_sol + " boxes"); */ return(my_sol); } public static void main(String[] args) { int n1, n2, n3; int s1, s2, s3; int Cap; int out; n1 = 1; s1 = 2; n2 = 7; s2 = 3; n3 = 2; s3 = 7; Cap = 18; out = solveBP(n1, s1, n2, s2, n3, s3, Cap, Cap); System.out.println("# boxes = " + out); } }