์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step14] 2580 : ์Šค๋„์ฟ  (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 22. 16:19

www.acmicpc.net/problem/2580

 

2580๋ฒˆ: ์Šค๋„์ฟ 

์Šค๋„์ฟ ๋Š” 18์„ธ๊ธฐ ์Šค์œ„์Šค ์ˆ˜ํ•™์ž๊ฐ€ ๋งŒ๋“  '๋ผํ‹ด ์‚ฌ๊ฐํ˜•'์ด๋ž‘ ํผ์ฆ์—์„œ ์œ ๋ž˜ํ•œ ๊ฒƒ์œผ๋กœ ํ˜„์žฌ ๋งŽ์€ ์ธ๊ธฐ๋ฅผ ๋ˆ„๋ฆฌ๊ณ  ์žˆ๋‹ค. ์ด ๊ฒŒ์ž„์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฐ€๋กœ, ์„ธ๋กœ ๊ฐ๊ฐ 9๊ฐœ์”ฉ ์ด 81๊ฐœ์˜ ์ž‘์€ ์นธ์œผ๋กœ ์ด๋ฃจ

www.acmicpc.net


์•„๋‹ˆ ๋‚˜ ์ง„์งœ ๋Œ์•„๋ฒ„๋ฆฌ๊ฒ ์Œ

์˜ˆ์ œ๋Š” ๋งž๋Š”๋ฐ ๊ณ„์† ์–ด๋–ป๊ฒŒ ๊ณ ์ณ๋„ ํ‹€๋ ธ๋‚˜๊ณ  ๋‚˜์™€์„œ ์ง„์งœ

์˜›๋‚ ์— ํ’€๋˜ ์Šค๋„์ฟ  ์ฑ… ์ฐพ์•„์„œ ์ œ์ผ ์–ด๋ ค์šด ๋ฌธ์ œ๋“ค๋กœ ํ•ด๋ดค๋Š”๋ฐ ๋‹ค ์ •๋‹ต ์ž˜ ๋‚˜์˜ค๋Š”๋ฐ

 

๋Œ€์ฒด

๋Œ€์ฒด

๋Œ€์ฒด์ฒดใ…”ใ…”์—์—ฅ

์–ด๋””๊ฐ€ ํ‹€๋ ธ๋‹ค๋Š”๊ฑฐ์ž„ ๋Œ์•„๋ฒ„๋ ค ์ง„์งœ

๋ฐฑ์ค€ ์ด๋†ˆ์ž์‹๋“ค์€ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์˜ค๋ฐฑ๊ฐœ์”ฉ ๋‚ด๋†”๋ผ... ์ง„์งœ ๋‹ค ๋•Œ๋ ค ๋ถ€์ˆ˜๋Ÿฌ ๊ฐ€๊ธฐ ์ „์—.....

์™œ ํ‹€๋ฆฐ๊ฑด์ง€๋ผ๋„ ์ข€ ์•Œ๋ ค์ค˜ ์ œ๋ฐ”๋ผ์•„์•„ใ…์•„๋ผ๋ž„

 

์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ฒ ๋‹ค.. ํ‹€๋ ธ๋‹ค์ž–์•„ ์  ์žฅ ใ…œ

# 2580.py

def check(x, y, num):
    ch_row = sudoku[x]
    ch_col = [sudoku[i][y] for i in range(9)]
    a = (x // 3) * 3
    b = (y // 3) * 3
    ch_block = sudoku[a][b:b + 3] + sudoku[a + 1][b:b + 3] + sudoku[a + 2][b:b + 3]
    if num in ch_row:
        return False
    elif num in ch_col:
        return False
    elif num in ch_block:
        return False
    else:
        return True


def sdk(zeros):
    if not zeros:
        for i in sudoku:
            print(' '.join(map(str, i)))
        return

    for t in range(1, 10):
        i, j = zeros.pop()
        if check(i, j, str(t)):
            sudoku[i][j] = str(t)
            sdk(zeros)
            sudoku[i][j] = '0'
        zeros.append([i, j])


sudoku = [[str(i) for i in input().split()] for _ in range(9)]
myzeros = [(i, j) for i in range(9) for j in range(9) if sudoku[i][j] == '0']
sdk(myzeros)

 

 

 

+ ๋ฐฑ์ค€ ์งˆ๋ฌธ ์˜ฌ๋ ธ๋Š”๋ฐ ์ „๋ถ€ 0์œผ๋กœ ์ฑ„์›Œ์ง„ ๋ฌธ์ œ ์ž…๋ ฅํ–ˆ์„๋•Œ ๋‹ต์ด ํ•˜๋‚˜๋งŒ ๋‚˜์˜ค๊ณ  ๋ฉˆ์ถ”๋ƒ๊ณ  ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹ฌ...

๊ทธ๋ž˜์„œ ํ•ด๋ดค๋”๋‹ˆ ๋ชจ๋“  ์กฐํ•ฉ ๋‹ค ์ถœ๋ ฅ๋˜๋Š”๋“ฏ ใ…Žใ…‹ ํ•˜๋‚˜ ํ”„๋ฆฐํŠธ ๋˜๋ฉด ๊ฐ•์ข…ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ผ๋‹จ ํ•ด๊ฒฐ^^..ํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ..

๋‚˜์ค‘์— ์–ธ์  ๊ฐ€ ํ’€์–ด๋ณผ๊ฒŒ ๋ฏธ์•ˆํ•˜๋‹ค ์Šค๋„์ฟ ์•ผ...

# 2580.py

import sys


def check(x, y, num):
    ch_row = sudoku[x]
    ch_col = [sudoku[i][y] for i in range(9)]
    a = (x // 3) * 3
    b = (y // 3) * 3
    ch_block = sudoku[a][b:b + 3] + sudoku[a + 1][b:b + 3] + sudoku[a + 2][b:b + 3]
    if num in ch_row:
        return False
    elif num in ch_col:
        return False
    elif num in ch_block:
        return False
    else:
        return True


def sdk(zeros):
    if not zeros:
        for i in sudoku:
            print(' '.join(map(str, i)))
        sys.exit()
        return

    for t in range(1, 10):
        i, j = zeros.pop()
        if check(i, j, str(t)):
            sudoku[i][j] = str(t)
            sdk(zeros)
            sudoku[i][j] = '0'
        zeros.append([i, j])


sudoku = [[str(i) for i in input().split()] for _ in range(9)]
myzeros = [(i, j) for i in range(9) for j in range(9) if sudoku[i][j] == '0']
sdk(myzeros)