Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why my memo array gets initialized to 0 java?

memo array is reinitialized to 0

I do not understand why my memo is set to 0 again even though i initialize to -1, it keeps resetting the memo to 0. Below code keeps calling it self, but in the very first level of stack, memo gets initialized to 0.

import java.util.Arrays;

public class RGBTree {
    public static void main(String[] args) {
        RGBTree rgb = new RGBTree();
        String[] G =
                {"..B.BB...RB..","......R..B.G.",
                        "B.......BB...",".......R...G.",
                        "B....GRB..R..","B...G.RG.R...",
                        ".R..RR..B.RB.","...RBG...G...",
                        "..B...B......","RBB..R.G....R",
                        "B...R.R......",".G.G..B.....R",".........R.R."};
        System.out.println(rgb.exist(G));

    }


    public String exist(String[] G) {
        int[][][][]  memo = new int[(1 << 13)][13][13][13];
        int n = G.length;
        int k = (n - 1) / 3;
        for (int i = 0; i < 1 << 13; i++) {
            for (int j = 0; j < 13; j++) {
                for (int l = 0; l < 13; l++) {
                    for (int m = 0; m < 13; m++) {
                        memo[i][j][k][l] = -1;
                    }
                }
            }
        }


        return (f(n, 1, k, 0, 0, 0, G, memo)) ? "Exist" : "Does not";

    }

    private boolean f(int v, int mask, int k, int r, int g, int b, String[] graph, int[][][][]memo) {

        boolean res = false;
        if (memo[mask][r][g][b] != -1) {
            res = memo[mask][r][g][b] == 1 ? true : false;
        } else {
            if (r == k && g == k && b == k) {
                res = true;
            }
            for (int i = 0; i < v; i++) {
                if (((mask & (1 << i)) == 1)) {
                    for (int j = 0; j < v; j++) {
                        if ((mask & (1 << j)) == 0 && graph[i].charAt(j) != '.') {
                            int nr = r, ng = g, nb = b;
                            char c = graph[i].charAt(j);
                            switch (c) {
                                case 'R':
                                    nr++;
                                    break;
                                case 'G':
                                    ng++;
                                    break;
                                case 'B':
                                    nb++;
                                    break;

                            }


                            if (nr <= k && ng <= k && nb <= k) {
                                res |= f(v, mask | (1 << j), k, nr, ng, nb, graph, memo);
                            }
                        }

                    }
                }
            }
            memo[mask][r][g][b] = res ? 1 : 2;

        }
        return res;

    }
}
like image 844
Giovani Salazar Avatar asked Jan 31 '26 11:01

Giovani Salazar


1 Answers

Your loop indexes are i, j, l and m but you used i, j, k and l in your initialization. Since k doesn't change in the loop, you aren't initializing every element in memo. You could do something like,

int[][][][] memo = new int[(1 << 13)][13][13][13];
for (int i = 0; i < memo.length; i++) {
    for (int j = 0; j < memo[i].length; j++) {
        for (int l = 0; l < memo[i][j].length; l++) {
            Arrays.fill(memo[i][j][l], -1);
        }
    }
}
like image 166
Elliott Frisch Avatar answered Feb 03 '26 03:02

Elliott Frisch



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!