Notice
Recent Posts
Recent Comments
Links
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array 객체
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journey🌳😊
The number of solutions to the 8 queens puzzle 본문
🌳Coursework ✨/Discrete Maths
The number of solutions to the 8 queens puzzle
yjyuwisely 2024. 1. 12. 07:00Modify the code considered in the lectures to find out the number of solutions to the 8 queens puzzle. For example, as was explained in the videos, for the 4 queens puzzle there are 2 solutions.
import itertools as it
def is_solution(perm):
for (i1, i2) in it.combinations(range(len(perm)),2):
if abs(i1-i2)==abs(perm[i1]-perm[i2]):
return False
return True
assert(is_solution([1,3,0,2]) == True)
assert(is_solution([3,1,0,2]) == False)
print(is_solution([1,3,0,2])) # should print True
print(is_solution([3,1,0,2])) # should print False
True
False
Solution)
def count_n_queens_solutions(n):
def is_valid(board):
current_queen_row, current_queen_col = len(board) - 1, board[-1]
for row, col in enumerate(board[:-1]):
diff = abs(current_queen_col - col)
if diff == 0 or diff == current_queen_row - row:
return False
return True
def solve(board):
if len(board) == n:
return 1
count = 0
for col in range(n):
board.append(col)
if is_valid(board):
count += solve(board)
board.pop()
return count
return solve([])
# Finding the number of solutions for the 8 queens puzzle
num_solutions_8_queens = count_n_queens_solutions(8)
num_solutions_8_queens
The answer is 92.
728x90
반응형
'🌳Coursework ✨ > Discrete Maths' 카테고리의 다른 글
Comments