Skip to content

Instantly share code, notes, and snippets.

@machkouroke
Last active April 14, 2023 15:44
Show Gist options
  • Save machkouroke/82b518db6c3c3be2b11c6e7e6b5dd0e9 to your computer and use it in GitHub Desktop.
Save machkouroke/82b518db6c3c3be2b11c6e7e6b5dd0e9 to your computer and use it in GitHub Desktop.
N_queen problem in julia
struct Queen
x::Int
y::Int
end
function Base.show(io::IO, q::Queen)::Nothing
print(io, "Queen($(q.x), $(q.y))")
end
function same_line(q1::Queen, queens::Vector{Queen})::Int
return sum(same_line(q1, q2) for q2 in queens)
end
function same_column(q1::Queen, q2::Queen)
return q1.y == q2.y
end
function same_column(q1::Queen, queens::Vector{Queen})::Int
return sum(same_column(q1, q2) for q2 in queens)
end
function same_diagonal(q1::Queen, q2::Queen)
return abs(q1.x - q2.x) == abs(q1.y - q2.y) || (q1.x - q2.x) == (q2.y - q1.y)
end
function same_diagonal(q1::Queen, queens::Vector{Queen})::Int
return sum(same_diagonal(q1, q2) for q2 in queens)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment