-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from HYU-PS-STUDY/leGit-y
[week-06-implementation] 1966, 2239
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from collections import deque | ||
|
||
def printer(q,index,M): | ||
cnt = 0 | ||
|
||
while q: | ||
nq = q.popleft() | ||
ni = index.popleft() | ||
if q and nq < max(q): | ||
q.append(nq) | ||
index.append(ni) | ||
continue | ||
cnt += 1 | ||
if ni == M: | ||
return cnt | ||
|
||
|
||
T = int(input()) | ||
for _ in range(T): | ||
N, M = map(int, input().split()) | ||
|
||
index = deque([i for i in range(N)]) | ||
priority = deque(list(map(int, input().split()))) | ||
|
||
print(printer(priority,index, M)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
def column_valid(row,value): | ||
|
||
for j in range(9): | ||
if sudoku[row][j] == value: | ||
return 0 | ||
|
||
return 1 | ||
|
||
|
||
|
||
def row_valid(column,value): | ||
|
||
for i in range(9): | ||
if sudoku[i][column] == value: | ||
return 0 | ||
|
||
return 1 | ||
|
||
|
||
def square_valid(i,j,value): | ||
|
||
i = i // 3 * 3 | ||
j = j // 3 * 3 | ||
|
||
for a in range(i, i + 3): | ||
for b in range(j, j + 3): | ||
if sudoku[a][b] == value: | ||
return 0 | ||
|
||
return 1 | ||
|
||
def dfs(cnt): | ||
if cnt == len(zero): | ||
return True | ||
|
||
r, c = zero[cnt] | ||
for i in range(1,10): | ||
if column_valid(r,i) and row_valid(c,i) and square_valid(r,c,i): | ||
sudoku[r][c] = i | ||
check = dfs(cnt + 1) | ||
if check: | ||
return True | ||
sudoku[r][c] = 0 | ||
|
||
|
||
|
||
sudoku = [list(map(int, input())) for _ in range(9)] | ||
zero = [] | ||
for i in range(9): | ||
for j in range(9): | ||
if sudoku[i][j] == 0: | ||
zero.append((i,j)) | ||
|
||
dfs(0) | ||
for i in range(9): | ||
for j in range(9): | ||
print(sudoku[i][j], end='') | ||
print() | ||
|
||
|