BOJ - 2580 - 스도쿠

Updated:

import sys

def isHorizontaol(x, value, sudoku):
    if value in sudoku[x]: return False
    else: return True

def isVertical(y, value, sudoku):
    for i in range(9):
        if value == sudoku[i][y]: return False
    return True

def isSquare(x, y, value, sudoku):

    X = x // 3 * 3
    Y = y // 3 * 3

    for i in range(X, X + 3):
        for j in range(Y, Y + 3):
            if value == sudoku[i][j]: return False
    return True

def DFS(sudoku, check, index):

    if index == len(check):
        for i in range(9):
            for j in range(9):
                print(sudoku[i][j], end=" ")
            print()
        sys.exit(0)

    for i in range(1, 10):
        nx = check[index][0]
        ny = check[index][1]


        if isHorizontaol(nx, i, sudoku) and isVertical(ny, i, sudoku) and isSquare(nx, ny, i, sudoku):
            sudoku[nx][ny] = i
            DFS(sudoku, check, index + 1)
            sudoku[nx][ny] = 0

def solution():
    check = []
    sudoku = []
    for _ in range(9):
        sudoku.append(list(map(int, sys.stdin.readline().split())))

    for i in range(9):
        for j in range(9):
            if sudoku[i][j] == 0:
                check.append((i, j))

    DFS(sudoku, check, 0)

solution()

https://www.acmicpc.net/problem/2580

Categories:

Updated:

Leave a comment