Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java find "stain" in multidimensional - recursive

Tags:

java

I am trying to solve this Recursive Exercise:

In a multidimensional board (M x N), which everyone of his elements can be empty or full.

"stain" size is the number of elements who are next to each other and has the value 'x'.

for example this is a multidimensional array (the numbers are the row/column number)

  | 0 | 1 | 2 | 3 | 4 |
0 |   | x |   |   | x |
1 | x |   |   | x | x |
2 |   |   | x | x |   |
3 | x |   |   |   |   |
4 | x | x | x |   |   |

There are 3 stains

  1. Rows (0,1), (1,0) - Size of 2
  2. Rows (0 ,4) ,(1 ,3) ,(1 ,4) ,(2 ,2) ,(2 ,3) - Size 5
  3. Rows (3 ,0) ,(4 ,0) ,(4 ,1) ,(4 ,2) - Size 4

We need to write a recursive method who has a signature of:

public static int stain (char [][] mat, int row, int col) 

the method will get a row and a column and calculate the stain size from that place, if there is no stain it will return 0.

I tried to write the method to solve it but looks like I was doing it kinda messy... can you direct me to the right direction? I'm not looking for a straight answer.

Thanks.

few remarks:

  • You can change the array in order to solve it.
  • You can use overloading
  • You cant use loops at all
  • You cant use static variables

My code:

function stain (char[][] mat, int row, int col) 
{
    int col_len = mat.length;
    int row_len = mat[0].length;
    if (row < 0 || col < 0 || row >= row_len || col >= col_len)
        return 0;

    if (mat[row][col] != 'x')
        return 0;

    mat[row][col] = 'y';

    // Count current
    int count = 1;
    // Go Left
    count += stain (mat, row, col-1);
    // Go Right
    count += stain (mat, row, col+1);
    // Go Up
    count += stain (mat, row+1, col);
    // Go Down
    count += stain (mat, row-1, col);

    // Go UpperRight
    count += stain (mat, row+1, col+1);
    // Go UpperLeft
    count += stain (mat, row-1, col+1);

    // Go DownRight
    count += stain (mat, row+1, col-1);
    // Go DownLeft
    count += stain (mat, row-1, col-1);

    return count;
}
like image 796
D_R Avatar asked Jan 26 '26 21:01

D_R


1 Answers

You cant use loops at all

Unfortunately, since this is the case, you're not going to get much better than what you have already. The code for recursing through all neighbors could be simplified to the following, which keeps the spirit of the restrictions despite violating them:

for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
        // This calls the function on the middle cell again. That's fine.
        count += stain(mat, row + dx, col + dy);
    }
}

Since you can't use loops, though, you really do need to explicitly recurse 8 times with slightly different parameters. This is bug-prone and a pain, but oh well.

like image 103
user2357112 supports Monica Avatar answered Jan 29 '26 10:01

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!