일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 | 31 |
Tags
- Decorator
- Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- concurrency
- DWG
- Python
- data science
- Substring with Concatenation of All Words
- Protocol
- kaggle
- Class
- 운영체제
- shiba
- 시바견
- Python Implementation
- 파이썬
- 109. Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- Generator
- t1
- Python Code
- LeetCode
- 프로그래머스
- 밴픽
- attribute
- Regular Expression
- 715. Range Module
- iterator
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
Archives
- Today
- Total
Scribbling
37. Sudoku Solver 본문
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
num_left = 0
for i in range(9):
for j in range(9):
if board[i][j] == '.':
num_left += 1
self.solver(board, num_left)
def solver(self, board, num_left):
if num_left == 0:
return True
# Search Blank -> (i, j)
start_y, start_x = 0, 0
found_flag = False
for t in range(1, 10):
for i in range(start_y, start_y+3):
for j in range(start_x, start_x+3):
if board[i][j] == '.':
found_flag = True
break
if found_flag:
break
if found_flag:
break
start_x += 3
if t % 3 == 0:
start_y += 3
start_x = 0
candidates = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
excludes = set()
# Square
start_y, start_x = i // 3 * 3, j // 3 * 3
for y in range(start_y, start_y+3):
for x in range(start_x, start_x+3):
if board[y][x] != '.':
excludes.add(board[y][x])
# Horizontal
for x in range(9):
if board[i][x] != '.':
excludes.add(board[i][x])
# Vertical
for y in range(9):
if board[y][j] != '.':
excludes.add(board[y][j])
candidates = [candidate for candidate in candidates \
if candidate not in list(excludes)]
for num in candidates:
board[i][j] = num
if self.solver(board, num_left - 1):
return True
board[i][j] = '.'
return False
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 46. Permutations (0) | 2021.09.07 |
---|---|
LeetCode: 40. Combination Sum II (0) | 2021.09.07 |
36. Valid Sudoku (0) | 2021.09.06 |
44. Wildcard Matching (0) | 2021.09.06 |
LeetCode: 48. Rotate Image (0) | 2021.09.05 |