๐Ÿ’ป [์ฝ”ํ…Œ07] 12์žฅ ๋ฐฑํŠธ๋ž˜ํ‚น

๊น€๋ฏธ์—ฐยท2024๋…„ 3์›” 2์ผ
0

12์žฅ. ๋ฐฑํŠธ๋ž˜ํ‚น

12-1. ๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ ๋ฐฑํŠธ๋ž˜ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…

1) ๋ฐฑํŠธ๋ž˜ํ‚น(backtracking)์ด๋ž€?

  • ์–ด๋–ค ๊ฐ€๋Šฅ์„ฑ์ด ์—†๋Š” ๊ณณ์„ ์•Œ์•„๋ณด๊ณ  ๋˜๋Œ์•„ ๊ฐ€๋Š” ๊ฒƒ

2) ๋ฐฑํŠธ๋ž˜ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€?

  • ๊ฐ€๋Šฅ์„ฑ์ด ์—†๋Š” ๊ณณ์—์„œ๋Š” ๋˜๋Œ์•„๊ฐ€๊ณ , ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๊ณณ์„ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

3) ์œ ๋ง ํ•จ์ˆ˜๋ž€?

  • ๋ฐฑํŠธ๋ž˜ํ‚น์˜ ํ•ต์‹ฌ : ํ•ด๊ฐ€ ๋  ๊ฐ€๋Šฅ์„ฑ์„ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ
  • ์œ ๋ง ํ•จ์ˆ˜ : ํ•ด๊ฐ€ ๋  ๊ฐ€๋Šฅ์„ฑ์„ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด ์ •์˜ํ•œ ํ•จ์ˆ˜

    ๐ŸŽซ ๋ฐฑํŠธ๋ž˜ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณผ์ •
    1) ์œ ํšจํ•œ ํ•ด์˜ ์ง‘ํ•ฉ ์ •์˜
    2) ์œ„ ๋‹จ๊ณ„์—์„œ ์ •์˜ํ•œ ์ง‘ํ•œ์„ ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„
    3) ์œ ๋ง ํ•จ์ˆ˜ ์ •์˜
    4) ๋ฐฑํŠธ๋ž˜ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•˜์—ฌ ํ•ด ์ฐพ๊ธฐ

12-2. ๋ชธํ’€๊ธฐ ๋ฌธ์ œ

  • ๋ฌธ์ œ47_1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ˆซ์ž ์ค‘ ํ•ฉ์ด 10์ด ๋˜๋Š” ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ
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]]
  • ๋ฌธ์ œ48_์Šค๋„์ฟ  ํผ์ฆ
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, 9, 5, 0, 0, 0],
 [0, 9, 8, 0, 0, 0, 0, 6, 0],
 [8, 0, 0, 0, 6, 0, 0, 0, 3],
 [4, 0, 0, 8, 0, 3, 0, 0, 1],
 [7, 0, 0, 0, 2, 0, 0, 0, 6],
 [0, 6, 0, 0, 0, 0, 2, 8, 0],
 [0, 0, 0, 4, 1, 9, 0, 0, 5],
 [0, 0, 0, 0, 8, 0, 0, 7, 9],
])
)


'''
๋ฐ˜ํ™˜๊ฐ’ : 
[
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9],
]
'''



print(solution(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0,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