Skip to content

Commit c77bcd1

Browse files
committed
[Gold IV] Title: 스도쿠, Time: 372 ms, Memory: 69108 KB -BaekjoonHub
1 parent 8e012ec commit c77bcd1

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

백준/Gold/2580. 스도쿠/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 79516 KB, 시간: 392 ms
7+
메모리: 69108 KB, 시간: 372 ms
88

99
### 분류
1010

1111
백트래킹
1212

1313
### 제출 일자
1414

15-
2025년 1월 10일 19:36:28
15+
2025년 1월 10일 19:44:35
1616

1717
### 문제 설명
1818

백준/Gold/2580. 스도쿠/스도쿠.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Foundation
2-
31
typealias Pos = (x: Int, y: Int)
42
var emptyPlaceList = [Pos]()
53
var sudokuBoard: [[Int]] = (0..<9).map { _ in
@@ -36,21 +34,22 @@ func check(_ x: Int, _ y: Int, number: Int) -> Bool {
3634
return true
3735
}
3836

39-
func solveSudoku(x: Int, y: Int, _ count: Int) {
37+
func solveSudoku(_ count: Int) -> Bool {
38+
if count >= emptyPlaceList.count {
39+
let answer = sudokuBoard.map {
40+
return $0.map { "\($0)" }.joined(separator: " ")
41+
}.joined(separator: "\n")
42+
print(answer)
43+
return true
44+
}
45+
let x = emptyPlaceList[count].x, y = emptyPlaceList[count].y
4046
for i in 1...9 {
4147
if check(x, y, number: i) {
4248
sudokuBoard[y][x] = i
43-
if count >= emptyPlaceList.count {
44-
let answer = sudokuBoard.map {
45-
return $0.map { "\($0)" }.joined(separator: " ")
46-
}.joined(separator: "\n")
47-
print(answer)
48-
exit(0)
49-
}
50-
let pos = emptyPlaceList[count]
51-
solveSudoku(x: pos.x, y: pos.y, count+1)
49+
if solveSudoku(count+1) { return true }
5250
sudokuBoard[y][x] = 0
5351
}
5452
}
53+
return false
5554
}
56-
solveSudoku(x: emptyPlaceList[0].x, y: emptyPlaceList[0].y, 1)
55+
solveSudoku(0)

0 commit comments

Comments
 (0)