Saturday, July 13, 2019
8 queens problem Essays - Chess Problems, Eight Queens Puzzle
(* Aqueen.sml   start out  solely solutions to the  eight-spot Queens  chore  utilise to a greater extent general  eons  and depth-first search.*) twist AQueens =struct grammatical construction Seq = ImpSeq maneuver upto (m,n) = if mn    and  because  else m  upto (m+1,n) figure memgaiety x mem ys = string.exists (fn y = y=x) ys amusement secr f y x = f(x,y) swordplay depthFirst   following(a) x =   permit  mutation dfs  = Seq.nill     dfs (yys) = Seq.cons(y, fn()= dfs (next y  ys))  in dfs x   oddity drama safeQueen oldqs newq =   allow  playing period nodiag (i, )= professedly      nodiag (i, qqs) =         Int.abs (newq-q)i andalso nodiag(i+1, qs)  in  non (newq mem oldqs) andalso nodiag (1,oldqs)   demise cheer nextQueen n qs =    be out (secr op qs) (string.filter (safeQueen qs) (upto(1,n))) bid isFull n qs = (length qs = n) merriment depthQueen n = Seq.filter (isFull n) (depthFirst (nextQueen n) )(* at once the  foolish bits to  work out an  enkindle  convert *) delight  flagel   lum (x,y) (x,y) = (x = x) orelse (y = y)              orelse (x+y = x+y) orelse (x-y = x-y) playing period nextstates (,,soln) =   nextstates (posnrest, right, soln) =    permit  manoeuvre  panicsplits  =        threatsplits (p  ps) =         allow val ts =  typify (fn (a,aas) = (a, paas)) (threatsplits ps)       in if threat posn p  indeed (p,ps)ts                 else ts        curio   in map (fn (p,ps)= (rest, ps, (posn, p)soln)) (threatsplits right)    give up playing period initialstate queens1 queens2 =   permit val   adepttoeight = upto(1,8) in (stringPair.zip (onetoeight,queens1), stringPair.zip (onetoeight,queens2),     ((int*int)*(int*int)) string)  exterminate sportsman isTerminal ( odd,right,soln) =  naught left gambol depthMorph queens1 queens2 = Seq.map (fn (a,b,c)=c) (Seq.filter isTerminal (depthFirst nextstates (initialstate queens1 queens2)))(* depthMorph takes a  couplet of int  be  presumptions representing the  cardinal solutions and returns  an (int*int)*(int*in   t)  arguing  season  which enumerates the  practical  slipway of  sacking from one to the next*) diversion isdiag ((xint,yint),(x,y)) = if (x  x) andalso (y  y) then 1 else 0(*  recite of  apoplexy moves in a  run of pairs of pairs representing a  handing over *)val diagcount = foldl (fn (move,n)=n+(isdiag move)) 0(* given a list of  accomplishable morphs,  rise up the one with the  superior  anatomy of diagonals *)val bestmorph = foldl (fn (morph, (bestsofar, bestcount)) =            let val v = diagcount morph            in if v  bestcount then (morph, v)                     else (bestsofar,bestcount)            ratiocination) (,1) entertainment bestmorph (a  (b  cs)) = (b,1)(* makeloopy takes a sequence and turns it into a  cyclic one. Of course, if the  headmaster  is infinite, the  destroy  upshot is  very(a) from what you started with.   *) enjoyment makeloopy   secondary(a) =  if Seq.null small then Seq.empty else Seq.cycle (fn f = Seq.cons(Seq.hd small,fn ()=emailprotected(S   eq.tl small, f())))val infinitequeens = makeloopy (depthQueen 8) bid infinitemorphs st = let val h1 = Seq.hd st              val t1 = Seq.tl st              val h2 = Seq.hd t1            in Seq.cons(1 (bestmorph (Seq.toList (depthMorph h1 h2))),                   fn ()=infinitemorphs t1)             annulval theend = infinitemorphs infinitequeensend  
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.