Due to the specification of the Amb challenge, this, ironically (given the notion of ambiguous functions), only produces one solution not 92. make Path c n | valid c && safe c 1 = n:c where valid c | n =:= column = uniq c where uniq  = True uniq (c:cs) = n /= c && uniq cs safe  _ = True safe (c:cs) k = abs (n-c) /= k && safe cs (k 1) where abs x = if x -- N-queens puzzle implemented with "Distinct Choices" pattern-- Sergio Antoy-- Tue Sep 4 PDT 2001-- updated: Mon Sep 23 PDT 2002 import Integer queens x | y =:= permute x & void (capture y) = y where y free capture y = let l1,l2,l3,y1,y2 free in l1 [y1] l2 [y2] l3 =:= y & abs (y1-y2) =:= length l2 1 -- negation as failure (implemented by encapsulated search):void c = (findall \_-c) =:=  -- How does this permutation algorithm work?
It is trivial to update Amb (might be better called a backtracker rather than Amb too) but here it is just used to show how easy it is to go from a generate and prune Linq solution to a backtrack solution. columns and sees if the solution can be extended by adding a queen at row n of column k 1. -- Only the elements [0,1,...,n-1] can be permuted.-- The reason is that each element is used as an index in a list.-- A list, called store, of free variables of length n is created.-- Then, the n iterations described below are executed.-- At the i-th iteration, an element, say s,-- of the initial list is non-deterministically selected.-- This element is used as index in the store.-- The s-th variable of the store is unified with i.-- At the end of the iterations, the elements of the store-- are a permutation of [0,1,...,n-1], i.e., the elements-- are unique since two iterations cannot select the same index. Y = Y -- A solution is represented by a list of integers.-- The i-th integer in the list is the column of the board-- in which the queen in the i-th row is placed.-- Rows and columns are numbered from 1 to 8.-- For example, [4,2,7,3,6,8,5,1] is a solution where the-- the queen in row 1 is in column 4, etc.-- Any solution must be a permutation of [1,2,...,8].
Updating a variable in sas
Q.../**Return true if queen placement q[n] does not conflict withother queens q through q[n-1]*/is Consistent(List q, int n) return true;} /**Print out N-by-N placement of queens from permutation q in ASCII.
| ------------- 6 Queen : 4 7 Queen : 40 8 Queen : 92 9 Queen : 352 10 Queen : 724 11 Queen : 2680 12 Queen : 14200 n_queens:task().
With this, the number of possibilities is reduced from n! If two queens occupy the same diagonal, the line connecting them has slope 1 or ¯1 .
Conversely, if the line connecting two queens has slope 1 or ¯1 , the two queens share a diagonal.
* N-QUEENS PROBLEM 04/09/2015 MACRO &LAB XDECO ®,&TARGET&LAB B I&SYSNDX branch around work area P&SYSNDX DS 0D, PL8 packed W&SYSNDX DS CL13 char I&SYSNDX CVD ®, P&SYSNDX convert to decimal MVC W&SYSNDX,=X'40202020202020202020212060' nice mask EDMK W&SYSNDX, P&SYSNDX 2 edit and mark BCTR R1,0 locate the right place MVC 0(1, R1), W&SYSNDX 12 move the sign MVC &TARGET.(12), W&SYSNDX move to target MENDNQUEENS CSECT SAVE (14,12) save registers on entry BALR R12,0 establish addressability USING *, R12 set base register ST R13, SAVEA 4 link my SA-my SA LR R13, R11 set my SA pointer LA R7, LL l LA R6,1 i=1LOOPI LR R1, R6 do i=1 to l SLA R1,1 i*2 STH R6, A-2(R1) a(i)=i LA R6,1(R6) i=i 1 BCT R7, LOOPI loop do i OPENEM OPEN (OUTDCB, OUTPUT) open the printer file LA R9,1 n=1 start of loop LOOPN CH R9, L do n=1 to l BH ELOOPN if nn BH E80 then goto e80 LR R11, R10 j=i E50 LR R1, R10 i SLA R1,1 i*2 LA R6, A-2(R1) [email protected](i) LR R1, R11 j SLA R1,1 j*2 LA R7, A-2(R1) [email protected](j) MVC Z,0(R6) z=a(i) MVC Y,0(R7) y=a(j) LR R3, R10 i SH R3, Y -y AR R3, R9 p=i-y n LR R4, R10 i AH R4, Y y BCTR R4,0 q=i y-1 MVC 0(2, R6), Y a(i)=y MVC 0(2, R7), Z a(j)=z LR R1, R3 p SLA R1,1 p*2 LH R2, U-2(R1) u(p) LTR R2, R2 if u(p)0 BNE E60 then goto e60 LR R1, R10 i SLA R1,1 i*2 STH R11, S-2(R1) s(i)=j LA R0,1 r0=1 LR R1, R3 p SLA R1,1 p*2 STH R0, U-2(R1) u(p)=1 LR R1, R4 q AR R1, R5 q r SLA R1,1 (q r)*2 STH R0, U-2(R1) u(q r)=1 LA R10,1(R10) i=i 1 B E40 goto e40E60 LA R11,1(R11) j=j 1 CR R11, R9 if j ; "eight queens problem" benchmark test .radix 16 0 nop ; mov #scr,@#E800 mov #88C6,@#E802; clear the display RAM mov #scr,r0 mov #1E0,r1cls: clr (r0) sob r1,cls; display the initial counter value clr r3 mov #scr,r0 jsr pc,number; perform the test jsr pc,queens; display the counter mov #scr,r0 jsr pc,numberfinish: br finish ; display the character R1 at the screen address R0,; advance the pointer R0 to the next columnputc: mov r2,-(sp); R1 (* ****** ****** *)//// Solving N-queen puzzle//(* ****** ****** *)//// How to test:// ./queens// How to compile:// patscc -DATS_MEMALLOC_LIBC -o queens queens.dats//(* ****** ****** *)//#include"share/atspre_staload.hats"//#include"share/HATS/atspre_staload_libats_ML.hats"//(* ****** ****** *) funsolutions(N:int) = let//funshow( board: list0(int)) : void =( list0_foreachifsafe(i, i 1, i-1, xs)then let val xs = cons0(i, xs)in if col = N then show(xs) else loop(col 1, xs)end // end of [then])//in loop(1, nil0())end // end of [solutions] (* ****** ****** *) val () = solutions(8) (* ****** ****** *) implement main0() = () (* ****** ****** *) (* end of [queens.dats] *) Size% = 8 Cell% = 32 VDU 23,22, Size%*Cell%; Size%*Cell%; Cell%, Cell%,16,128 8,5 *font Arial Unicode MS,16 GCOL 3,11 FOR i% = 0 TO Size%-1 STEP 2 RECTANGLE FILL i%*Cell%*2,0, Cell%*2, Size%*Cell%*2 RECTANGLE FILL 0,i%*Cell%*2, Size%*Cell%*2, Cell%*2 NEXT num% = FNqueens(Size%, Cell%) SYS "Set Window Text", @hwnd%, "Total " STR$(num%) " solutions" REPEAT : WAIT 1 : UNTIL FALSE END DEF FNqueens(n%, s%) LOCAL i%, j%, m%, p%, q%, r%, a%(), b%(), c%() DIM a%(n%), b%(n%), c%(4*n%-2) FOR i% = 1 TO DIM(a%(),1) : a%(i%) = i% : NEXT m% = 0 i% = 1 j% = 0 r% = 2*n%-1 REPEAT i% -= 1 j% = 1 p% = 0 q% = -r% REPEAT i% = 1 c%(p%) = 1 c%(q% r%) = 1 SWAP a%(i%),a%(j%) p% = i% - a%(i%) n% q% = i% a%(i%) - 1 b%(i%) = j% j% = i% 1 UNTIL j% The following is a re-implementation of the algorithm given above but using the MC package that allows machine independent runtime generation of native machine code (currently only available for i386 machines).