fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int[][] land = {{1,2,3,5},{5,6,7,8},{4,3,2,1}};
  13. ArrayList<ArrayList<Integer>> arrList = new ArrayList<ArrayList<Integer>>();
  14.  
  15. getCase(land, new boolean[land[0].length], new ArrayList<Integer>(), arrList,0);
  16.  
  17. int max = 0;
  18. for(int i = 0 ; i < arrList.size() ; i++ ){
  19. int sum = 0;
  20. for(int j = 0 ; j < arrList.get(i).size() ; j++){
  21. sum += arrList.get(i).get(j);
  22. }
  23. if(max < sum){
  24. max = sum;
  25. }
  26. }
  27.  
  28. System.out.println(max);
  29. // your code goes here
  30. }
  31.  
  32. private static void getCase(int[][] land, boolean[] check, ArrayList<Integer> arr,ArrayList<ArrayList<Integer>> arrList,int index){
  33.  
  34. if(arr.size() == land.length){
  35. arrList.add(new ArrayList<>(arr));
  36. return ;
  37. }
  38.  
  39. for(int i = index ; i < land.length ; i++){
  40. for(int j = 0 ; j < land[i].length ; j++){
  41. if(!check[j]){
  42. arr.add(land[i][j]);
  43. check[j] = true;
  44.  
  45. getCase(land, check, arr, arrList,i+1);
  46.  
  47. arr.remove(arr.size() -1);
  48. check[j] = false;
  49.  
  50. }
  51. }
  52. }
  53.  
  54.  
  55. }
  56. }
Success #stdin #stdout 0.08s 54668KB
stdin
Standard input is empty
stdout
16