Q: Am I PCI compliant if I have an SSL certificate?
我不明白为什么我的备忘录再次设置为0,即使我初始化为-1,它仍然将备忘录重置为0.下面的代码一直调用它自己,但是在第一级堆栈中,备忘录被初始化到了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;
}
}
答案 0 :(得分:1)
您的循环索引为i
,j
,l
和m
,但您使用了i
,j
,k
和初始化中的l
。由于k
在循环中没有变化,因此您不会初始化memo
中的每个元素。你可以做点什么,
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);
}
}
}