baekjoon 20251220

This commit is contained in:
songyc macbook 2025-12-20 21:45:49 +09:00
parent 2523bdda53
commit 003d253779
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1 @@
print((lambda r1, s : 2*s - r1)(*map(int, input().split())))

View File

@ -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)