File tree Expand file tree Collapse file tree 4 files changed +106
-0
lines changed Expand file tree Collapse file tree 4 files changed +106
-0
lines changed Original file line number Diff line number Diff line change
1
+ from collections import deque
2
+
3
+ def print_board (s ,n ):
4
+ for i in s :
5
+ l = ""
6
+ for j in range (i - 1 ):
7
+ l += "."
8
+ l += "♛"
9
+ for j in range (n - i ):
10
+ l += "."
11
+ print (l )
12
+
13
+ def safe (s ):
14
+ l = len (s )
15
+ for i in range (l - 1 ):
16
+ qi = s [i ]
17
+ for j in range (1 ,l - i ):
18
+ qj = s [i + j ]
19
+ if (qj == qi ) or (qj == qi + j ) or (qj == qi - j ):
20
+ return False
21
+ return True
22
+
23
+ def search (s , n ):
24
+ rs = []
25
+ for i in range (1 ,n + 1 ):
26
+ if safe (s + tuple ([i ])):
27
+ rs .append (s + tuple ([i ]))
28
+ return rs
29
+
30
+ def solution (input ):
31
+ n = int (input )
32
+ count = 0
33
+ start = ()
34
+ queue = deque ([start ])
35
+ last = ""
36
+ while len (queue ) != 0 :
37
+ current = queue .popleft ()
38
+ for d in search (current , n ):
39
+ if len (d ) == n :
40
+ count += 1
41
+ last = d
42
+ else :
43
+ queue .append (d )
44
+ print_board (last , n )
45
+ return count
Original file line number Diff line number Diff line change
1
+ def safe (s ):
2
+ l = len (s )
3
+ for i in range (l - 1 ):
4
+ qi = s [i ]
5
+ for j in range (1 ,l - i ):
6
+ qj = s [i + j ]
7
+ if (qj == qi ) or (qj == qi + j ) or (qj == qi - j ):
8
+ return False
9
+ return True
10
+
11
+ def search (s , n ):
12
+ rs = 0
13
+ for i in range (1 ,n + 1 ):
14
+ next = (s + tuple ([i ]))
15
+ if safe (next ):
16
+ if len (next ) == n :
17
+ rs += 1
18
+ else :
19
+ rs += search (next , n )
20
+ return rs
21
+
22
+ def solution (input ):
23
+ n = int (input )
24
+ count = search (tuple (), n )
25
+ return count
Original file line number Diff line number Diff line change
1
+ def safe (s ):
2
+ l = len (s )
3
+ for i in range (l - 1 ):
4
+ qi = s [i ]
5
+ for j in range (1 ,l - i ):
6
+ qj = s [i + j ]
7
+ if (qj == qi ) or (qj == qi + j ) or (qj == qi - j ):
8
+ return False
9
+ return True
10
+
11
+ def rollback (board ):
12
+ board .pop (- 1 )
13
+ if len (board ) == 0 : return
14
+ board [- 1 ] += 1
15
+
16
+ def solution (input ):
17
+ n = int (input )
18
+ c = 0
19
+ board = [1 ]
20
+ i = 0
21
+ while len (board ) != 0 :
22
+ if board [- 1 ] > n :
23
+ rollback (board )
24
+ elif safe (board ):
25
+ if len (board ) == n :
26
+ rollback (board )
27
+ c += 1
28
+ else :
29
+ board += [1 ]
30
+ else :
31
+ if board [- 1 ] >= n :
32
+ rollback (board )
33
+ else :
34
+ board [- 1 ] += 1
35
+ return c
Original file line number Diff line number Diff line change
1
+ 8
You can’t perform that action at this time.
0 commit comments