tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
8
Task/N-queens-problem/Python/n-queens-problem-1.py
Normal file
8
Task/N-queens-problem/Python/n-queens-problem-1.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
from itertools import permutations
|
||||
|
||||
n = 8
|
||||
cols = range(n)
|
||||
for vec in permutations(cols):
|
||||
if n == len(set(vec[i]+i for i in cols)) \
|
||||
== len(set(vec[i]-i for i in cols)):
|
||||
print ( vec )
|
||||
2
Task/N-queens-problem/Python/n-queens-problem-2.py
Normal file
2
Task/N-queens-problem/Python/n-queens-problem-2.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def board(vec):
|
||||
print ("\n".join('.' * i + 'Q' + '.' * (n-i-1) for i in vec) + "\n===\n")
|
||||
17
Task/N-queens-problem/Python/n-queens-problem-3.py
Normal file
17
Task/N-queens-problem/Python/n-queens-problem-3.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# From: http://wiki.python.org/moin/SimplePrograms, with permission from the author, Steve Howell
|
||||
BOARD_SIZE = 8
|
||||
|
||||
def under_attack(col, queens):
|
||||
return col in queens or \
|
||||
any(abs(col - x) == len(queens)-i for i,x in enumerate(queens))
|
||||
|
||||
def solve(n):
|
||||
solutions = [[]]
|
||||
for row in range(n):
|
||||
solutions = [solution+[i+1]
|
||||
for solution in solutions
|
||||
for i in range(BOARD_SIZE)
|
||||
if not under_attack(i+1, solution)]
|
||||
return solutions
|
||||
|
||||
for answer in solve(BOARD_SIZE): print(list(enumerate(answer, start=1)))
|
||||
19
Task/N-queens-problem/Python/n-queens-problem-4.py
Normal file
19
Task/N-queens-problem/Python/n-queens-problem-4.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
BOARD_SIZE = 8
|
||||
|
||||
def under_attack(col, queens):
|
||||
return col in queens or \
|
||||
any(abs(col - x) == len(queens)-i for i,x in enumerate(queens))
|
||||
|
||||
def solve(n):
|
||||
solutions = [[]]
|
||||
for row in range(n):
|
||||
solutions = (solution+[i+1]
|
||||
for solution in solutions # first for clause is evaluated immediately,
|
||||
# so "solutions" is correctly captured
|
||||
for i in range(BOARD_SIZE)
|
||||
if not under_attack(i+1, solution))
|
||||
return solutions
|
||||
|
||||
answers = solve(BOARD_SIZE)
|
||||
first_answer = next(answers)
|
||||
print(list(enumerate(first_answer, start=1)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue