I am trying to solve the problem CERK07- KeyTask. I learnt a new concept here using bitmask in bfs. I am trying to implement my code in java . I am having problem in the if condtion when I encounter a door and I want to check whether have the key for that door. My IDE suggests that the the condition ((umask & 0x2) == 1 ) is always false. Same case for all the other doors. I don’t know why this is happening ?
`
public static int bfs(int[] start, char[][] grid, int[][][] d) {
int m = grid.length;
int n = grid[0].length;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{start[0], start[1], 0});
while (!q.isEmpty()) {
int[] curr = q.poll();
int x = curr[0];
int y = curr[1];
int umask = curr[2];
final int[][] shifts = {
{-1, 0},
{0, 1},
{1, 0},
{0, -1}
};
for (int[] shift : shifts) {
int dx = x + shift[0];
int dy = y + shift[1];
if (isValid(dx, dy, m, n) && grid[dx][dy] != '#') {
if (grid[dx][dy] == 'X') {
return d[x][y][umask] + 1;
}
if (d[dx][dy][umask] == -1) {
char val = grid[dx][dy];
if (val == 'r' || val == 'b' || val == 'g' || val == 'y') {
int vmask = umask;
if (val == 'r') vmask |= 0x1;
else if (val == 'b') vmask |= 0x2;
else if (val == 'g') vmask |= 0x4;
else vmask |= 0x8;
q.add(new int[]{dx, dy, vmask});
d[dx][dy][umask] = d[x][y][umask] + 1;
d[dx][dy][vmask] = d[dx][dy][umask];
} else if (val == 'R' || val == 'G' || val == 'B' || val == 'Y') {
switch (val) {
case 'R':
if ((umask & 0x1) == 1) q.add(new int[]{dx, dy, umask});
break;
case 'B':
if ((umask & 0x2) == 1) q.add(new int[]{dx, dy, umask});
break;
case 'G':
if ((umask & 0x4) == 1) q.add(new int[]{dx, dy, umask});
break;
case 'Y':
if ((umask & 0x8) == 1) q.add(new int[]{dx, dy, umask});
break;
}
d[dx][dy][umask] = d[x][y][umask] + 1;
} else {
q.add(new int[]{dx, dy, umask});
d[dx][dy][umask] = d[x][y][umask] + 1;
}
}
}
}
}
return -1;
}`