N-queens minimum and knights and bishops: Difference between revisions

J partial implementation
m (added Pascal only for Queens.)
(J partial implementation)
Line 301:
. . . . .
</pre>
=={{header|J}}==
 
A crude initial attempt -- brute force search with some minor state space pruning. I am not patient enough to run this for 9x9 and 10x10 boards for bishops and knights:
 
<lang J>genboard=: {{
safelen=:2*len=. {.y
shape=: 2$len
board=: shape$0
safeshape=: 2*shape
c=:,coords=: safeshape#.shape#:i.shape
qrow=. i:{.shape-1
qcol=. qrow*safelen
qdiag1=. qrow+qcol
qdiag2=. qrow-qcol
queen=: ~.qrow,qcol,qdiag1,qdiag2
k1=. ,(1 _1*safelen)+/2 _2
k2=. ,(2 _2*safelen)+/1 _1
knight=: 0,k1,k2
bishop=: ~.qdiag1,qdiag2
row=. i.len
first=: ~.,coords#"1~(row<:>.<:len%2) * >:/~ row
EMPTY
}}
 
place=: {{
N=. #r=. _#~#c
for_open.first do.
board=. coords e.y+open
if. 0 e.,board do.
seq=. open y place1 N board
if. N>:#seq do.
N=. <:#r=. seq
end.
else. open return.
end.
end.
r
}}
 
place1=: {{
r=. (n+2) {.!._ x
N=. n
todo=. (#~ ({:x) < ]) (,0=y)#c
lim=. >./,(first +&{: x)+m
for_open. (#~ lim>:]) todo do.
board=. y>.coords e.m+open
if. 0 e.,board do.
if. N>#x do.
seq=. x,open m place1 N board
if. N>:#seq do.
N=. <:#r=. seq
end.
end.
else. x,open return.
end.
end.
r
}}
 
task=: {{
Q=:K=:B=:i.0
echo label=. ' order queens bishops knights'
L=.1+#@>;:label
for_order.1+i.10 do.
genboard order
Q=: '.Q'{~coords e.q=. place queen
K=: '.K'{~coords e.k=. place knight
B=: '.B'{~coords e.b=. place bishop
echo (":Q;B;K),&.|:>(' Q: ',":#q);(' B: ',":#b);' K: ',":#k
end.
}}</lang>
 
Task output:
 
<lang J> task''
+-----+ Q: 1
¦Q¦B¦K¦ B: 1
+-----+ K: 1
+--------+ Q: 1
¦Q.¦BB¦KK¦ B: 2
¦..¦..¦KK¦ K: 4
+--------+
+-----------+ Q: 1
¦...¦...¦KKK¦ B: 3
¦.Q.¦BBB¦.K.¦ K: 4
¦...¦...¦...¦
+-----------+
+--------------+ Q: 3
¦Q...¦....¦....¦ B: 4
¦..Q.¦BBBB¦KKKK¦ K: 4
¦....¦....¦....¦
¦.Q..¦....¦....¦
+--------------+
+-----------------+ Q: 3
¦Q....¦B....¦K....¦ B: 5
¦...Q.¦..BB.¦.....¦ K: 5
¦.....¦B....¦..KK.¦
¦..Q..¦..B..¦..KK.¦
¦.....¦.....¦.....¦
+-----------------+
+--------------------+ Q: 4
¦Q.....¦B.....¦......¦ B: 6
¦.....Q¦..BBB.¦......¦ K: 8
¦.Q....¦......¦KK..KK¦
¦......¦......¦KK..KK¦
¦......¦..BB..¦......¦
¦..Q...¦......¦......¦
+--------------------+
+-----------------------+ Q: 5
¦Q......¦.......¦KKKK.K.¦ B: 7
¦..Q....¦B.BBB..¦.......¦ K: 13
¦....Q..¦.......¦.....K.¦
¦.Q.....¦...B...¦K....K.¦
¦...Q...¦.......¦.......¦
¦.......¦..BB...¦K.KK.K.¦
¦.......¦.......¦......K¦
+-----------------------+
+--------------------------+ Q: 5
¦Q.......¦........¦KKK.....¦ B: 8
¦..Q.....¦B..BBB..¦.....K..¦ K: 14
¦....Q...¦........¦....KK..¦
¦.Q......¦....B...¦K.......¦
¦...Q....¦....B...¦.......K¦
¦........¦........¦..KK...K¦
¦........¦...BB...¦..K.....¦
¦........¦........¦.....K.K¦
+--------------------------+
|attention interrupt: place</lang>
 
For 9 and 10 for queens:
<lang J> (":<'.Q'{~coords e. place q),&.|:,:' Q: ',":#q=. queen [(genboard 9)
┌─────────┐ Q: 65
│Q........│
│..Q......│
│......Q..│
│.........│
│.........│
│.Q.......│
│.....Q...│
│.........│
│.........│
└─────────┘
(":<'.Q'{~coords e. place q),&.|:,:' Q: ',":#q=. queen [(genboard 10)
┌──────────┐ Q: 73
│Q.........│
│..Q.......│
│........Q.│
│..........│
│..........│
│...Q......│
│.........Q│
│..........│
│.....Q....│
│..........│
└──────────┘
</lang>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
Line 494 ⟶ 651:
 
Real time: 45.856 s CPU share: 99.24 % // below the time limit of 60 secs</pre>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
6,951

edits