Queen.rb
class Queen def initialize() @v=0 end def arrCopy2(arr) arr0=Array.new(arr.length) for i in 0..arr.length-1 do arr0[i]=Array.new(arr[i]) end return arr0 end def addQueen(arr,x,y) setQueen(arr,x,y,1) end def setQueen(arr,x,y,n) for i in 0..arr.length-1 do arr[x][i]=n end for j in 0..arr[x].length-1 do arr[j][y]=n end x0=x y0=y while x0<arr.length and y0<arr[x0].length arr[x0][y0]=n x0=x0+1 y0=y0+1 end x0=x y0=y while x0>=0 and y0>=0 arr[x0][y0]=n x0=x0-1 y0=y0-1 end x0=x y0=y while x0>=0 and y0<arr[x0].length arr[x0][y0]=n x0=x0-1 y0=y0+1 end x0=x y0=y while x0<arr.length and y0>=0 arr[x0][y0]=n x0=x0+1 y0=y0-1 end return arr end def findQueen(arr,lst,x) if(x>=arr.length) @v=@v+1 puts("]>>solution No."+@v.to_s) pp lst return end for y in 0..arr[x].length-1 do if(arr[x][y]==0) lst.push(y) arr0=arrCopy2(arr) addQueen(arr,x,y) findQueen(arr,lst,x+1) arr=arr0 lst.pop end end end end
test.rb
require "pp" require "./Queen.rb" len = 8 arr=Array.new(len){Array.new(len, 0)} #fuck #arr = Array.new(len, Array.new(len,0)) lst = Array.new() q=Queen.new q.findQueen(arr,lst,0) puts("end")