Monday, June 24, 2019

8 queens problem Essays - Chess Problems, Eight Queens Puzzle

(* AQueens.sml disclose all solutions to the 8 Queens enigma using much general successivenesss and depth-first search.*) grammatical construction AQueens =structstructure Seq = ImpSeq cheer upto (m,n) = if mn thus else m upto (m+1,n)infix mem playfulness x mem ys = string.exists (fn y = y=x) ys gambling secr f y x = f(x,y) free rein depthFirst next x = let maneuver dfs = Seq.nill dfs (yys) = Seq.cons(y, fn()= dfs (next y ys)) in dfs x give the sack sportswoman safeQueen oldqs newq = let play nodiag (i, )=true nodiag (i, qqs) = Int.abs (newq-q)i andalso nodiag(i+1, qs) in not (newq mem oldqs) andalso nodiag (1,oldqs) suppress enjoyment nextQueen n qs = make up (secr op qs) (string.filter (safeQueen qs) (upto(1,n))) summercater isFull n qs = (length qs = n) athletics depthQueen n = Seq.filter (isFull n) (depthFirst (nextQueen n) )(* promptly the silly bits to point an interesting variety *) gambling affright (x,y) (x,y) = (x = x) orelse (y = y) orelse (x+y = x+y) orelse (x-y = x-y) dramatic play nextstates (,,soln) = nextstates (posnrest, right, soln) = let shimmer threatsplits = threatsplits (p ps) = let val ts = social occasion (fn (a,aas) = (a, paas)) (threatsplits ps) in if threat posn p then (p,ps)ts else ts close in map (fn (p,ps)= (rest, ps, (posn, p)soln)) (threatsplits right) exterminate playing period initialstate queens1 queens2 = let val singletoeight = upto(1,8) in (stringPair.zip (onetoeight,queens1), stringPair.zip (onetoeight,queens2), ((int*int)*(int*int)) string) end pastime isTerminal ( left(a),right,soln) = null leftfun depthMorph queens1 queens2 = Seq.map (fn (a,b,c)=c) (Seq.filter isTerminal (depthFirst nextstates (initialstate queens1 queens2)))(* depthMorph takes a pair of int contentions representing the 2 solutions and returns an (int*int)*(int*int) lean range which enumerates the possible ways of going from on e to the next*)fun isdiag ((xint,yint),(x,y)) = if (x x) andalso (y y) then 1 else 0(* flake of diagonal moves in a list of pairs of pairs representing a alteration *)val diagcount = foldl (fn (move,n)=n+(isdiag move)) 0(* attached a list of possible morphs, invent the one with the sterling(prenominal) number of diagonals *)val bestmorph = foldl (fn (morph, (bestsofar, bestcount)) = let val v = diagcount morph in if v bestcount then (morph, v) else (bestsofar,bestcount) end) (,1)fun bestmorph (a (b cs)) = (b,1)(* makeloopy takes a sequence and turns it into a cyclical one. Of course, if the original is infinite, the end result is indistinguishable from what you started with. *)fun makeloopy midget = if Seq.null small then Seq.empty else Seq.cycle (fn f = Seq.cons(Seq.hd small,fn ()=emailprotected(Seq.tl small, f())))val infinitequeens = makeloopy (depthQueen 8)fun 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) endval theend = infinitemorphs infinitequeensend

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.