๐ซ ๋ฐฑํธ๋ํน ์๊ณ ๋ฆฌ์ฆ ๊ณผ์
1) ์ ํจํ ํด์ ์งํฉ ์ ์
2) ์ ๋จ๊ณ์์ ์ ์ํ ์งํ์ ๊ทธ๋ํ๋ก ํํ
3) ์ ๋ง ํจ์ ์ ์
4) ๋ฐฑํธ๋ํน ์๊ณ ๋ฆฌ์ฆ์ ํ์ฉํ์ฌ ํด ์ฐพ๊ธฐ
def solution(N):
results = []
def backtrack(sum, selected_nums, start):
if sum == 10:
results.append(selected_nums)
return
for i in range(start, N+1):
if sum + i <= 10:
backtrack(sum+i, selected_nums+[i], i+1)
backtrack(0, [], 1)
return results
# TEST ์ฝ๋ ์
๋๋ค. ์ฃผ์์ ํ๊ณ ์คํ์์ผ๋ณด์ธ์
print(solution(5)) # ๋ฐํ๊ฐ : [[1, 2, 3, 4], [1, 4, 5], [2, 3, 5]]
print(solution(2)) # ๋ฐํ๊ฐ : []
print(solution(7)) # ๋ฐํ๊ฐ : [[1, 2, 3, 4], [1, 2, 7], [1, 3, 6], [1, 4, 5], [2, 3, 5], [3, 7], [4, 6]]
def solution(board):
def is_valid(num, row, col):
return not(in_row(num, row) or in_col(num, col) or in_box(num, row, col))
def in_row(num, row):
return num in board[row]
def in_col(num, col):
return num in (board[i][col] for i in range(9))
def in_box(num, row, col):
box_row = (row // 3) * 3
box_col = (col // 3) * 3
for i in range(box_row, box_row + 3):
for j in range(box_col, box_col + 3):
if board[i][j] == num:
return True
return False
def find_empty_position():
for i in range(9):
for j in range(9):
if board[i][j] == 0:
return i, j
return None
def find_solution():
empty_pos = find_empty_position()
if not empty_pos:
return True
row, col = empty_pos
for num in range(1, 10):
if is_valid(num, row, col):
board[row][col] = num
if find_solution():
return True
board[row][col] = 0
return False
find_solution()
return board
# TEST ์ฝ๋ ์
๋๋ค. ์ฃผ์์ ํ๊ณ ์คํ์์ผ๋ณด์ธ์
print(solution(
[
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1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