← Back to context

Comment by jinlisp

6 months ago

Solution in J (a few lines of code from a novice J programmer).

   board =: 3 : '? (y,y) $ y'
   test =: 4 : 0
   m =. x
   n =. #x
   n -: # ~. ({&m) <"1 (i. n) ,. 0 A. (i. n)
   )
   findsol =:3 : 0
   ns =. 1 i.~ (y & test)"0 i. !#y
   if. (ns = !#y) do. 'No solution found'
   else.
   ns A. i. #y
   end.
   )
  
   m =: board 5
   m
   4 4 0 1 1
   3 2 0 1 1
   2 4 1 1 0
   3 2 1 3 4
   2 0 2 2 0
   0 1 2 3 4
   findsol m
   0 1 2 3 4

  echo 'So the solution found is putting the queens   in the diagonal'