N Queens

Show all solutions of n-queens.
Output 1302 if queens' positions are [(0, 1), (1, 3), (2, 0), (3, 2)].
One solution per one line.
Solutions should be sorted as string.
4 <= N <= 10

IN

4

OUT

1302
2031

IN

5

OUT

02413
03142
13024
14203
20314
24130
30241
31420
41302
42031

IN

6

OUT

135024
251403
304152
420531

IN

7

OUT

0246135
0362514
0415263
0531642
1306425
1350246
1403625
1420635
1463025
1526304
1642053
2051463
2053164
2461350
2514036
2613504
2630415
3025164
3041526
3164205
3502461
3625140
3641502
4036251
4053162
4152630
4205316
4613502
4615203
5024613
5140362
5203641
5246031
5263041
5316420
5360241
6135024
6251403
6304152
6420531

IN

8

OUT

04752613
05726314
06357142
06471352
13572064
14602753
14630752
15063724
15720364
16257403
16470352
17502463
20647135
24170635
24175360
24603175
24730615
25147063
25160374
25164073
25307461
25317460
25703641
25704613
25713064
26174035
26175304
27360514
30471625
30475261
31475026
31625704
31625740
31640752
31746025
31750246
35041726
35716024
35720641
36074152
36271405
36415027
36420571
37025164
37046152
37420615
40357162
40731625
40752613
41357206
41362750
41506372
41703625
42057136
42061753
42736051
46027531
46031752
46137025
46152037
46152073
46302751
47302516
47306152
50417263
51602473
51603742
52064713
52073164
52074136
52460317
52470316
52613704
52617403
52630714
53047162
53174602
53602417
53607142
57130642
60275314
61307425
61520374
62057413
62714053
63147025
63175024
64205713
71306425
71420635
72051463
73025164
Login to submit solution


45 symbols pyth
Sat Nov 12 04:19:48 2016 Sait2000
Vum+hd],Hedf.Am**JahdHKaedeT-JKhT*GUQQ]YjkeMN
49 symbols pyth
Fri Oct 21 09:02:28 2016 Sait2000
Vum+hd],Hedf.Am***J-hdHK-edeT+JK-JKhT*GUQQ]YjkeMN
54 symbols pyth
Fri Oct 21 08:30:00 2016 Sait2000
=]YVQ=Ym+hd],Nedf.Am***J-hdNK-edeT+JK-JKhT*YUQ)VYjkeMN
58 symbols pyth
Mon Sep 19 18:19:24 2016 Sait2000
L?bf.Am***J-hdhhTK-edehT+JK-JKtTm+],tbhded*UQytb]YVyQjkeMN
151 symbols python2
Fri Oct 21 07:25:25 2016 Sait2000
N=range(input())
z=[],
for r in N:z=[s+[(-r,c)]for s in z for c in N if~-any(j in(r+i+j,c,c+r+i,c-i-r)for i,j in s)]
for s in z:print`map(max,s)`[1::3]
156 symbols python2
Fri Oct 21 07:21:45 2016 Sait2000
N=range(int(input()))
z=[],
for r in N:z=[s+[(-r,c)]for s in z for c in N if~-any(j in(r+i+j,c,c+r+i,c-i-r)for i,j in s)]
for s in z:print`map(max,s)`[1::3]
158 symbols perl
Sun Oct 25 00:11:39 2015 vakorol
#!/usr/bin/perl -ln
$,='';sub f{my($a,$r)=@_;@$a-$_||print@$a;for$c(0..$_-1){my($i,$b);for(@$a){$b=1,last if$c==$_||abs$c-$_==$r-$i++}!$b&&f($A=[@$a,$c],$r+1)&&return$A}}f([])
158 symbols python3
Thu Oct 20 04:43:34 2016 Sait2000
N,*z=range(int(input())),[]
for r in N:z=[s+[(-r,c)]for s in z for c in N if~-any(j in(r+i+j,c,c+r+i,c-i-r)for i,j in s)]
for s in z:print(*map(max,s),sep='')
163 symbols python3
Thu Oct 20 04:35:29 2016 Sait2000
N,*z=range(int(input())),[]
for r in N:z=[s+[(r,c)]for s in z for c in N if~-any(j in(r-i+j,c,c+r-i,c+i-r)for i,j in s)]
for s in z:print(*(j for i,j in s),sep='')
166 symbols python3
Thu Oct 20 04:32:46 2016 Sait2000
N,*z=range(int(input())),[]
for r in N:z=[s+[(r,c)]for s in z for c in N if~-any(j in(r-i+j,c,c+r-i,c+i-r)for i,j in s)]
for s in z:print(''.join(str(j)for i,j in s))
169 symbols python3
Thu Oct 20 04:27:09 2016 Sait2000
N,*z=range(int(input())),[]
for r in N:z=[s+[(r,c)]for s in z for c in N if~-any(0in(r-i,c-j,c-j+r-i,c-j+i-r)for i,j in s)]
for s in z:print(''.join(str(j)for i,j in s))
171 symbols python3
Thu Oct 20 04:23:59 2016 Sait2000
N,*z=range(int(input())),[]
for r in N:z=[s+[(r,c)]for s in z for c in N if~-any(0in(r-i,c-j)or c-j in(r-i,i-r)for i,j in s)]
for s in z:print(''.join(str(j)for i,j in s))
172 symbols python3
Thu Oct 20 04:21:20 2016 Sait2000
N,*z=range(int(input())),[]
for r in N:z=[s+[(r,c)]for s in z for c in N if~-any(r==i or c==j or c-j in(r-i,i-r)for i,j in s)]
for s in z:print(''.join(str(j)for i,j in s))
176 symbols perl
Sat Oct 24 21:16:53 2015 vakorol
#!/usr/bin/perl -ln
sub f{my($a,$r)=@_;scalar@$a==$_&&print join'',@$a;for$c(0..$_-1){my($i,$b);for(@$a){$b=1,last if$c==$_||abs($c-$_)==$r-$i++}f($A=[@$a,$c],$r+1)&&return$A if!$b}return}f([])
178 symbols haskell
Wed Oct 21 09:15:52 2015 YoshikuniJujo
import Data.List
main=interact$unlines.map(>>=show).(\n->f n n[][][]).read
f _ 0 _ _ _=[[]]
f n i l h s=[q:r|q<-[0..n-1]\\(l++h++s),r<-f n(i-1)(fmap pred$q:l)(q:h)(fmap(+1)$q:s)]
183 symbols python3
Thu Oct 20 04:08:19 2016 Sait2000
N,*z=range(int(input())),[]
for r in N:z=[s+[(r,c)]for s in z for c in N if~-any(r==i or c==j or c-j in(r-i,i-r)for i,j in s)]
print("\n".join("".join(str(q[1])for q in s)for s in z))
184 symbols python3
Thu Oct 20 04:04:43 2016 Sait2000
N=range(int(input()))
z=[[]]
for r in N:z=[s+[(r,c)]for s in z for c in N if~-any(r==i or c==j or c-j in(r-i,i-r)for i,j in s)]
print("\n".join("".join(str(q[1])for q in s)for s in z))
198 symbols python3
Thu Oct 20 03:57:49 2016 Sait2000
N=int(input())
z=[[]]
for r in range(N):z=[s+[(r,c)]for s in z for c in range(N)if~-any(r==i or c==j or c-j in(r-i,i-r)for i,j in s)]
print("\n".join(sorted("".join(str(q[1])for q in s)for s in z)))
225 symbols python3
Thu Oct 20 03:55:34 2016 Sait2000
N=int(input())
a=lambda r,c,i,j:r==i or c==j or c-j in(r-i,i-r)
z=[[]]
for n in range(N):z=[s+[(n,c)]for s in z for c in range(N)if~-any(a(*(n,c)+q)for q in s)]
print("\n".join(sorted("".join(str(q[1])for q in s)for s in z)))
236 symbols python3
Thu Oct 20 03:49:37 2016 Sait2000
N=int(input())
a=lambda r,c,i,j:r==i or c==j or c-j in(r-i,i-r)
z=lambda n:[s+[[n-1,c]]for s in z(n-1)for c in range(N)if~-any(a(*([n-1,c]+q))for q in s)]if n>0else[[]]
print("\n".join(sorted("".join(str(q[1])for q in s)for s in z(N))))

View all solutions