The 4 Queens Problem consists in placing four queens on a 4 x 4 chessboard so that no two queens attack each other. That is, no two queens are allowed to be placed on the same row , the same column or the same diagonal .

```
constraint c2{
box.sum() == 4;
foreach (box[i,j]) {
box[i][j] inside {[0:1]};
foreach (box[ii,jj]) {
((ii == i) && (jj != j) && (box[i][j] == 1)) -> (box[i][j] != box[ii][jj]);
((ii != i) && (jj == j) && (box[i][j] == 1)) -> (box[i][j] != box[ii][jj]);
((ii+jj == i+j) && (i!=j) && (box[i][j] == 1)) -> (box[i][j] != box[ii][jj]);
(((ii-i == jj-j)||(i-ii == j-jj)) && (box[i][j] == 1)) -> (box[i][j] != box[ii][jj]);
}
}
}
```

But this doesn’t give the correct array!

The constraints for your diagonals are not correct.

```
module top;
class A;
rand bit box[4][4];
constraint c2{
box.sum(R) with (R.sum(C) with (int'(C))) == 4;
foreach (box[i,j])
foreach (box[ii,jj]) {
ii == i && jj != j && box[i][j] -> !box[ii][jj];
ii != i && jj == j && box[i][j] -> !box[ii][jj];
ii+jj == i+j && i != ii && box[i][j] -> !box[ii][jj];
ii-jj == i-j && i != ii && box[i][j] -> !box[ii][jj];
}
}
endclass
A a =new;
initial repeat (4) begin
assert(a.randomize());
foreach(a.box[i]) $display("%p",a.box[i]);
$display;
end
endmodule
```

Thanks Dave! I missed the last statement. Thanks for your response!

Got error for “box.sum(R) with (R.sum(C) with (int’(C))) == 4;”

Replace the same with “box.sum() with (int’(item))== 4;”. It worked and got desired array!

```
module top;
class A;
rand bit box[4][4];
constraint c2{
box.sum() with (int'(item))== 4;
foreach (box[i,j])
foreach (box[ii,jj]) {
ii == i && jj != j && box[i][j] -> !box[ii][jj];
ii != i && jj == j && box[i][j] -> !box[ii][jj];
ii+jj == i+j && i != ii && box[i][j] -> !box[ii][jj];
ii-jj == i-j && i != ii && box[i][j] -> !box[ii][jj];
}
}
endclass
A a =new;
initial repeat (4) begin
assert(a.randomize());
foreach(a.box[i]) $display("%p",a.box[i]);
$display;
end
endmodule
```

0 0 1 0

1 0 0 0

0 0 0 1

0 1 0 0