baekjoon 20251220
This commit is contained in:
parent
2523bdda53
commit
003d253779
1
code_study/Baekjoon/python/3046.py
Normal file
1
code_study/Baekjoon/python/3046.py
Normal file
@ -0,0 +1 @@
|
||||
print((lambda r1, s : 2*s - r1)(*map(int, input().split())))
|
||||
84
code_study/Baekjoon/swift/2239.swift
Normal file
84
code_study/Baekjoon/swift/2239.swift
Normal 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)
|
||||
Loading…
x
Reference in New Issue
Block a user