Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
682 B
1
Indexable
Never
private static boolean isSafe(int[][] matrix, int[] color, int i, int c) {
        int row = i / matrix.length;
        int col = i % matrix.length;

        for (int j = -1; j <= 1; j++) {
            for (int k = -1; k <= 1; k++) {
                if (j != 0 || k != 0) {
                    int newRow = row + j;
                    int newCol = col + k;

                    if (newRow >= 0 && newRow < matrix.length && newCol >= 0 && newCol < matrix.length && matrix[newRow][newCol] == 1 && color[newRow * matrix.length + (c + j + 4) % 4] == c) {
                        return false;
                    }
                }
            }
        }

        return true;
    }