Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class test2018moed87question1Recuersion
- {
- public static int countRopes(int [][] mat) {
- return countRopes(mat, 0, 0);
- }
- private static int countRopes(int [][] mat, int row, int col) {
- if (col == mat[0].length)
- return 0;
- int count = isRope(mat, 0, col+1, mat[0][col]);
- return count + countRopes(mat, 0, col+1);
- }
- private static int isRope(int [][] mat, int row, int col, int prev) {
- if (row == mat.length)
- return 1;
- if (row < 0 || col < 0 || row > mat.length || col > mat[0].length-1 || mat[row][col] == 0)
- return 0;
- if (mat[row][col] > prev)
- return 0;
- int temp = mat[row][col];
- int sum = isRope(mat, row+1, col-1, mat[row][col]) +
- isRope(mat, row+1, col, mat[row][col]) +
- isRope(mat, row+1, col+1, mat[row][col]);
- mat[row][col] = temp;
- return sum;
- }
- public static void main (String[] args){
- int [][] mat = {{10,10,10,10,10,10},
- {20,20,8,20,9,20},
- {30,6,30,30,30,7},
- {3,40,4,40,40,5},
- {1,2,50,50,50,2}};
- System.out.println(countRopes(mat));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement