Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
59
Task/N-queens-problem/MUMPS/n-queens-problem-1.mumps
Normal file
59
Task/N-queens-problem/MUMPS/n-queens-problem-1.mumps
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
Queens New count,flip,row,sol
|
||||
Set sol=0
|
||||
For row(1)=1:1:4 Do try(2) ; Not 8, the other 4 are symmetric...
|
||||
;
|
||||
; Remove symmetric solutions
|
||||
Set sol="" For Set sol=$Order(sol(sol)) Quit:sol="" Do
|
||||
. New xx,yy
|
||||
. Kill sol($Translate(sol,12345678,87654321)) ; Vertical flip
|
||||
. Kill sol($Reverse(sol)) ; Horizontal flip
|
||||
. Set flip="--------" for xx=1:1:8 Do ; Flip over top left to bottom right diagonal
|
||||
. . New nx,ny
|
||||
. . Set yy=$Extract(sol,xx),nx=8+1-xx,ny=8+1-yy
|
||||
. . Set $Extract(flip,ny)=nx
|
||||
. . Quit
|
||||
. Kill sol(flip)
|
||||
. Set flip="--------" for xx=1:1:8 Do ; Flip over top right to bottom left diagonal
|
||||
. . New nx,ny
|
||||
. . Set yy=$Extract(sol,xx),nx=xx,ny=yy
|
||||
. . Set $Extract(flip,ny)=nx
|
||||
. . Quit
|
||||
. Kill sol(flip)
|
||||
. Quit
|
||||
;
|
||||
; Display remaining solutions
|
||||
Set count=0,sol="" For Set sol=$Order(sol(sol)) Quit:sol="" Do Quit:sol=""
|
||||
. New s1,s2,s3,txt,x,y
|
||||
. Set s1=sol,s2=$Order(sol(s1)),s3="" Set:s2'="" s3=$Order(sol(s2))
|
||||
. Set txt="+--+--+--+--+--+--+--+--+"
|
||||
. Write !," ",txt Write:s2'="" " ",txt Write:s3'="" " ",txt
|
||||
. For y=8:-1:1 Do
|
||||
. . Write !,y," |"
|
||||
. . For x=1:1:8 Write $Select($Extract(s1,x)=y:" Q",x+y#2:" ",1:"##"),"|"
|
||||
. . If s2'="" Write " |"
|
||||
. . If s2'="" For x=1:1:8 Write $Select($Extract(s2,x)=y:" Q",x+y#2:" ",1:"##"),"|"
|
||||
. . If s3'="" Write " |"
|
||||
. . If s3'="" For x=1:1:8 Write $Select($Extract(s3,x)=y:" Q",x+y#2:" ",1:"##"),"|"
|
||||
. . Write !," ",txt Write:s2'="" " ",txt Write:s3'="" " ",txt
|
||||
. . Quit
|
||||
. Set txt=" A B C D E F G H"
|
||||
. Write !," ",txt Write:s2'="" " ",txt Write:s3'="" " ",txt Write !
|
||||
. Set sol=s3
|
||||
. Quit
|
||||
Quit
|
||||
try(col) New ok,pcol
|
||||
If col>8 Do Quit
|
||||
. New out,x
|
||||
. Set out="" For x=1:1:8 Set out=out_row(x)
|
||||
. Set sol(out)=1
|
||||
. Quit
|
||||
For row(col)=1:1:8 Do
|
||||
. Set ok=1
|
||||
. For pcol=1:1:col-1 If row(pcol)=row(col) Set ok=0 Quit
|
||||
. Quit:'ok
|
||||
. For pcol=1:1:col-1 If col-pcol=$Translate(row(pcol)-row(col),"-") Set ok=0 Quit
|
||||
. Quit:'ok
|
||||
. Do try(col+1)
|
||||
. Quit
|
||||
Quit
|
||||
Do Queens
|
||||
|
|
@ -1,63 +1,3 @@
|
|||
Queens New count,flip,row,sol
|
||||
Set sol=0
|
||||
For row(1)=1:1:4 Do try(2) ; Not 8, the other 4 are symmetric...
|
||||
;
|
||||
; Remove symmetric solutions
|
||||
Set sol="" For Set sol=$Order(sol(sol)) Quit:sol="" Do
|
||||
. New xx,yy
|
||||
. Kill sol($Translate(sol,12345678,87654321)) ; Vertical flip
|
||||
. Kill sol($Reverse(sol)) ; Horizontal flip
|
||||
. Set flip="--------" for xx=1:1:8 Do ; Flip over top left to bottom right diagonal
|
||||
. . New nx,ny
|
||||
. . Set yy=$Extract(sol,xx),nx=8+1-xx,ny=8+1-yy
|
||||
. . Set $Extract(flip,ny)=nx
|
||||
. . Quit
|
||||
. Kill sol(flip)
|
||||
. Set flip="--------" for xx=1:1:8 Do ; Flip over top right to bottom left diagonal
|
||||
. . New nx,ny
|
||||
. . Set yy=$Extract(sol,xx),nx=xx,ny=yy
|
||||
. . Set $Extract(flip,ny)=nx
|
||||
. . Quit
|
||||
. Kill sol(flip)
|
||||
. Quit
|
||||
;
|
||||
; Display remaining solutions
|
||||
Set count=0,sol="" For Set sol=$Order(sol(sol)) Quit:sol="" Do Quit:sol=""
|
||||
. New s1,s2,s3,txt,x,y
|
||||
. Set s1=sol,s2=$Order(sol(s1)),s3="" Set:s2'="" s3=$Order(sol(s2))
|
||||
. Set txt="+--+--+--+--+--+--+--+--+"
|
||||
. Write !," ",txt Write:s2'="" " ",txt Write:s3'="" " ",txt
|
||||
. For y=8:-1:1 Do
|
||||
. . Write !,y," |"
|
||||
. . For x=1:1:8 Write $Select($Extract(s1,x)=y:" Q",x+y#2:" ",1:"##"),"|"
|
||||
. . If s2'="" Write " |"
|
||||
. . If s2'="" For x=1:1:8 Write $Select($Extract(s2,x)=y:" Q",x+y#2:" ",1:"##"),"|"
|
||||
. . If s3'="" Write " |"
|
||||
. . If s3'="" For x=1:1:8 Write $Select($Extract(s3,x)=y:" Q",x+y#2:" ",1:"##"),"|"
|
||||
. . Write !," ",txt Write:s2'="" " ",txt Write:s3'="" " ",txt
|
||||
. . Quit
|
||||
. Set txt=" A B C D E F G H"
|
||||
. Write !," ",txt Write:s2'="" " ",txt Write:s3'="" " ",txt Write !
|
||||
. Set sol=s3
|
||||
. Quit
|
||||
Quit
|
||||
try(col) New ok,pcol
|
||||
If col>8 Do Quit
|
||||
. New out,x
|
||||
. Set out="" For x=1:1:8 Set out=out_row(x)
|
||||
. Set sol(out)=1
|
||||
. Quit
|
||||
For row(col)=1:1:8 Do
|
||||
. Set ok=1
|
||||
. For pcol=1:1:col-1 If row(pcol)=row(col) Set ok=0 Quit
|
||||
. Quit:'ok
|
||||
. For pcol=1:1:col-1 If col-pcol=$Translate(row(pcol)-row(col),"-") Set ok=0 Quit
|
||||
. Quit:'ok
|
||||
. Do try(col+1)
|
||||
. Quit
|
||||
Quit
|
||||
Do Queens
|
||||
|
||||
+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+
|
||||
8 | |##| Q|##| |##| |##| | |##| Q|##| |##| |##| | |##| | Q| |##| |##|
|
||||
+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+
|
||||
Loading…
Add table
Add a link
Reference in a new issue