baekjoon 20251208
This commit is contained in:
parent
831a6dcace
commit
4aa7d1cdd3
49
code_study/Baekjoon/python/2239.py
Normal file
49
code_study/Baekjoon/python/2239.py
Normal file
@ -0,0 +1,49 @@
|
||||
import sys
|
||||
input = sys.stdin.readline
|
||||
|
||||
board = []
|
||||
emptyAxis = []
|
||||
|
||||
row_check = [[False]*10 for _ in range(9)]
|
||||
col_check = [[False]*10 for _ in range(9)]
|
||||
square_check = [[False]*10 for _ in range(9)]
|
||||
|
||||
def square_idx(r, c) :
|
||||
return (r//3)*3 + c//3
|
||||
|
||||
def backTracking(depth) :
|
||||
if depth == len(emptyAxis) :
|
||||
for row in board :
|
||||
print("".join(map(str, row)))
|
||||
exit(0)
|
||||
|
||||
row, col = emptyAxis[depth]
|
||||
sqr_idx = square_idx(row, col)
|
||||
|
||||
for num in range(1, 10) :
|
||||
if not row_check[row][num] and not col_check[col][num] and not square_check[sqr_idx][num] :
|
||||
board[row][col] = num
|
||||
row_check[row][num] = True
|
||||
col_check[col][num] = True
|
||||
square_check[sqr_idx][num] = True
|
||||
|
||||
backTracking(depth+1)
|
||||
|
||||
board[row][col] = 0
|
||||
row_check[row][num] = False
|
||||
col_check[col][num] = False
|
||||
square_check[square_idx(row, col)][num] = False
|
||||
|
||||
for i in range(9) :
|
||||
row = list(map(int, list(input().strip())))
|
||||
board.append(row)
|
||||
for j in range(9) :
|
||||
num = row[j]
|
||||
if num == 0 :
|
||||
emptyAxis.append((i,j))
|
||||
else :
|
||||
row_check[i][num] = True
|
||||
col_check[j][num] = True
|
||||
square_check[square_idx(i,j)][num] = True
|
||||
|
||||
backTracking(0)
|
||||
29
code_study/Baekjoon/python/2252.py
Normal file
29
code_study/Baekjoon/python/2252.py
Normal file
@ -0,0 +1,29 @@
|
||||
import heapq
|
||||
import sys
|
||||
input = sys.stdin.readline
|
||||
|
||||
N, M = map(int, input().split())
|
||||
|
||||
graph = [[] for _ in range(N+1)]
|
||||
inDegree = [0]*(N+1)
|
||||
for _ in range(M) :
|
||||
u, v = map(int, input().split())
|
||||
graph[u].append(v)
|
||||
inDegree[v] += 1
|
||||
|
||||
pq = []
|
||||
for i in range(1,N+1) :
|
||||
if inDegree[i] == 0 :
|
||||
heapq.heappush(pq, i)
|
||||
|
||||
result = []
|
||||
while len(pq) :
|
||||
current = heapq.heappop(pq)
|
||||
result.append(str(current))
|
||||
|
||||
for next in graph[current] :
|
||||
inDegree[next] -= 1
|
||||
if inDegree[next] == 0 :
|
||||
heapq.heappush(pq, next)
|
||||
|
||||
print(" ".join(result))
|
||||
Loading…
x
Reference in New Issue
Block a user