Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class test2018moed87question1Recuersion
  4. {
  5. public static int countRopes(int [][] mat) {
  6. return countRopes(mat, 0, 0);
  7. }
  8. private static int countRopes(int [][] mat, int row, int col) {
  9. if (col == mat[0].length)
  10. return 0;
  11.  
  12. int count = isRope(mat, 0, col+1, mat[0][col]);
  13. return count + countRopes(mat, 0, col+1);
  14. }
  15.  
  16. private static int isRope(int [][] mat, int row, int col, int prev) {
  17. if (row == mat.length)
  18. return 1;
  19. if (row < 0 || col < 0 || row > mat.length || col > mat[0].length-1 || mat[row][col] == 0)
  20. return 0;
  21. if (mat[row][col] > prev)
  22. return 0;
  23. int temp = mat[row][col];
  24. int sum = isRope(mat, row+1, col-1, mat[row][col]) +
  25. isRope(mat, row+1, col, mat[row][col]) +
  26. isRope(mat, row+1, col+1, mat[row][col]);
  27. mat[row][col] = temp;
  28. return sum;
  29. }
  30.  
  31. public static void main (String[] args){
  32. int [][] mat = {{10,10,10,10,10,10},
  33. {20,20,8,20,9,20},
  34. {30,6,30,30,30,7},
  35. {3,40,4,40,40,5},
  36. {1,2,50,50,50,2}};
  37. System.out.println(countRopes(mat));
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement