N-queens problem: Difference between revisions
Content added Content deleted
Line 2,122: | Line 2,122: | ||
def solve(size: Int) = (1 until size).foldLeft(seed(size)) (expand(_, size, _))</lang> |
def solve(size: Int) = (1 until size).foldLeft(seed(size)) (expand(_, size, _))</lang> |
||
=={{header|SystemVerilog}}== |
|||
Create a random board configuration, with the 8-queens as a constraint |
|||
<lang SystemVerilog>program N_queens; |
|||
parameter SIZE_LOG2 = 3; |
|||
parameter SIZE = 1 << SIZE_LOG2; |
|||
`define ABS_DIFF(a,b) (a>b?a-b:b-a) |
|||
class board; |
|||
rand bit [SIZE_LOG2-1:0] row[SIZE]; |
|||
constraint rook_moves { |
|||
foreach (row[i]) foreach (row[j]) if (i < j) { |
|||
row[i] != row[j]; |
|||
} |
|||
} |
|||
constraint diagonal_moves { |
|||
foreach (row[i]) foreach (row[j]) if (i < j) { |
|||
`ABS_DIFF(row[i], row[j]) != `ABS_DIFF(i,j); |
|||
} |
|||
} |
|||
function void next; |
|||
randomize; |
|||
foreach (row[i]) begin |
|||
automatic bit [SIZE-1:0] x = 1 << row[i]; |
|||
$display( " %b", x ); |
|||
end |
|||
$display("--"); |
|||
endfunction |
|||
endclass |
|||
board b = new; |
|||
initial repeat(1) b.next; |
|||
endprogram |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |