# How to save the queen?

**URL:** <https://discourse.julialang.org/t/how-to-save-the-queen/98024>\
**Category:** General Usage\
**Created:** [April 28, 2023, 7:46am UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024 "2023-04-28T07:46:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 28, 2023, 7:46am UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/1 "2023-04-28T07:46:44Z")

</div>

I’m looking for (but haven’t found yet) a way to save the solutions of the 8 queens problem on a chessboard into an array of some sort, without necessarily having to “print” them live.  
If possible by changing the following functions. But it’s also fine with a completely different setting.

for the moment my workaround is this

```julia
function safe(queen, row)
   for i in 1:queen-1
     xriga = T[i]
        if (xriga == row||
            xriga == row- (queen- i) ||
            xriga == row+ (queen- i))
            return false
        end
    end
    return true
end

function try(k)
    if (k <= 8)
        for i in 1:8
            if (safe(k, i))
                T[k] = i
                try(k + 1)
            end
        end
    else
        for i in 1:8
            print(io,T[i],' ')
        end
        print(io,'\n')
    end
end
io=IOBuffer();
T=fill(0,8)
res=Vector{Vector{Int}}()
try(1)

sol=take!(io)

reshape(sol,17,:)'[:,1:2:16].-48

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 28, 2023, 8:29am UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/2 "2023-04-28T08:29:09Z")

</div>

Hi @rocco_sprmnt21!  
It may be silly of me but I think people would find it easier to help you if your code was in English?  
As for storage, I would suggest an 8x8 bool (sparse) matrix for each solution?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 28, 2023, 8:54am UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/3 "2023-04-28T08:54:12Z")

</div>

I edited the code to make it easier to read.  
I found a way to overcome the problem I had: the resulting vector filled via the `push!(res,T)` statement had all values equal to the last T entered.  
Changing to `push!(res,copy(T))` seems to have overcome the problem.

```julia
function try(k)
    if (k <= 8)
        for i in 1:8
            if (safe(k, i))
                T[k] = i
                try(k + 1)
            end
        end
    else
        # for i in 1:8
        # print(io,T[i],' ')
        # end
        # print(io,'\n')
        push!(res,copy(T))
    end
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 28, 2023, 9:00am UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/4 "2023-04-28T09:00:55Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 28, 2023, 9:02am UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/5 "2023-04-28T09:02:28Z")

</div>

the answer is good 😆

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 28, 2023, 7:31pm UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/6 "2023-04-28T19:31:57Z")

</div>

I think this question might be a tad late (w.r.t. the queen). But here is a late answer:

```julia
function tryqueens2(k)
           if (k <= 8)
               push!(res2,0)
               for i in 1:8
                   if (safe(k, i))
                       T[k] = i
                       res2[end] = i
                       tryqueens2(k + 1)
                   end
               end
               pop!(res2)
               if length(res2)>0 && res2[end]<0 && k>1
                   res2[end] -= 1
                   push!(res2,0)
               end
           else
               push!(res2,-1)
               push!(res2,0)
           end
       end

```

This version stores the solutions similarly to a pre-order traversal of the search tree used to find them. It is quite compact in terms of memory, and thus would be good for speed. It is easy enough to iterate over the solutions, but randomly accessing them or sampling would be harder.

Basic operation:

```julia
julia> res2 = Vector{Int8}(); tryqueens2(1)

julia> foldl((x,y)->(y>0 ? push!(x,y) : 
  (println(x); deleteat!(x,length(x)+y+1:length(x)); x)), 
  res2; init=Int[])
[1, 5, 8, 6, 3, 7, 2, 4]
[1, 6, 8, 3, 7, 4, 2, 5]
[1, 7, 4, 6, 8, 2, 5, 3]
[1, 7, 5, 8, 2, 4, 6, 3]
[2, 4, 6, 8, 3, 1, 7, 5]
[2, 5, 7, 1, 3, 8, 6, 4]
[2, 5, 7, 4, 1, 8, 6, 3]
[2, 6, 1, 7, 4, 8, 3, 5]
:
:

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 28, 2023, 7:46pm UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/7 "2023-04-28T19:46:20Z")

</div>

Linking some related Rosetta code on the [Eight Queens puzzle](https://rosettacode.org/wiki/N-queens_problem#Julia).

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 28, 2023, 8:07pm UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/8 "2023-04-28T20:07:49Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [April 28, 2023, 8:27pm UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/9 "2023-04-28T20:27:28Z")

</div>

I’ll just leave this here as an example of how not to approach this: [Fun "interview" question solved easily in Julia - #13 by Mason](https://discourse.julialang.org/t/fun-interview-question-solved-easily-in-julia/96693/13)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 28, 2023, 8:29pm UTC](https://discourse.julialang.org/t/how-to-save-the-queen/98024/10 "2023-04-28T20:29:06Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.
