From 003d2537795b3dc852b3ce0e622eceb60b46daca Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Sat, 20 Dec 2025 21:45:49 +0900 Subject: [PATCH] baekjoon 20251220 --- code_study/Baekjoon/python/3046.py | 1 + code_study/Baekjoon/swift/2239.swift | 84 ++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 code_study/Baekjoon/python/3046.py create mode 100644 code_study/Baekjoon/swift/2239.swift diff --git a/code_study/Baekjoon/python/3046.py b/code_study/Baekjoon/python/3046.py new file mode 100644 index 0000000..cd4a721 --- /dev/null +++ b/code_study/Baekjoon/python/3046.py @@ -0,0 +1 @@ +print((lambda r1, s : 2*s - r1)(*map(int, input().split()))) \ No newline at end of file diff --git a/code_study/Baekjoon/swift/2239.swift b/code_study/Baekjoon/swift/2239.swift new file mode 100644 index 0000000..8c1db4d --- /dev/null +++ b/code_study/Baekjoon/swift/2239.swift @@ -0,0 +1,84 @@ +import Foundation + +var rowCheck: [[Bool]] = Array(repeating: Array(repeating: false, count: 10), count: 9) +var colCheck: [[Bool]] = Array(repeating: Array(repeating: false, count: 10), count: 9) +var squareCheck: [[Bool]] = Array(repeating: Array(repeating: false, count: 10), count: 9) + +let squareArea = {(row: Int, col: Int) -> Int in + return (row/3)*3 + (col/3) +} + +var board: [[Int]] = [] +var emptyAxis: [(Int, Int)] = [] + +for row in 0..<9 { + if let input = readLine(), + let line = input.compactMap({Int(String($0))}) as? [Int], + line.count == 9 + { + for col in 0..<9 { + let num = line[col] + + if num != 0 { + let square = squareArea(row, col) + rowCheck[row][num] = true + colCheck[col][num] = true + squareCheck[square][num] = true + } + else { + emptyAxis.append((row, col)) + } + } + + board.append(line) + } +} + +func isPossible(row: Int, col: Int, num: Int) -> Bool { + if rowCheck[row][num] { + return false + } + + if colCheck[col][num] { + return false + } + + if squareCheck[squareArea(row, col)][num] { + return false + } + + return true +} + +func backTracking(_ depth: Int) { + if depth == emptyAxis.count { + for row in board { + for num in row { + print(num, terminator: "") + } + print() + } + + exit(0) + } + + let (row, col) = emptyAxis[depth] + + for num in 1...9 { + if isPossible(row: row, col: col, num: num) { + board[row][col] = num + rowCheck[row][num] = true + colCheck[col][num] = true + squareCheck[squareArea(row, col)][num] = true + + backTracking(depth+1) + + board[row][col] = 0 + rowCheck[row][num] = false + colCheck[col][num] = false + squareCheck[squareArea(row, col)][num] = false + } + } +} + +backTracking(0)