Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
68
Task/N-queens-problem/Eiffel/n-queens-problem.e
Normal file
68
Task/N-queens-problem/Eiffel/n-queens-problem.e
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
class
|
||||
QUEENS
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE}
|
||||
counter: INTEGER
|
||||
|
||||
place_queens(board: ARRAY[INTEGER]; level: INTEGER)
|
||||
local
|
||||
i, j: INTEGER
|
||||
safe: BOOLEAN
|
||||
do
|
||||
if level > board.count
|
||||
then
|
||||
counter := counter + 1
|
||||
else
|
||||
from
|
||||
i := 1
|
||||
until
|
||||
i > board.count
|
||||
loop
|
||||
safe := True
|
||||
from
|
||||
j := 1
|
||||
until
|
||||
j = level or not safe
|
||||
loop
|
||||
if (board[j] = i)
|
||||
or (j - level = i - board[j])
|
||||
or (j - level = board[j] - i)
|
||||
then
|
||||
safe := False
|
||||
end
|
||||
j := j + 1
|
||||
end
|
||||
if safe
|
||||
then
|
||||
board[level] := i
|
||||
place_queens(board, level + 1)
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
feature
|
||||
possible_positions_of_n_queens(n: INTEGER): INTEGER
|
||||
local
|
||||
board: ARRAY[INTEGER]
|
||||
do
|
||||
create board.make_filled (0, 1, n)
|
||||
counter := 0
|
||||
place_queens(board, 1)
|
||||
Result := counter
|
||||
end
|
||||
|
||||
make
|
||||
local
|
||||
n: INTEGER
|
||||
do
|
||||
io.put_string ("Please enter the number of queens: ")
|
||||
io.read_integer
|
||||
n := io.last_integer
|
||||
print("%NPossible number of placings: " + possible_positions_of_n_queens(n).out + "%N")
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue