Skip to content

Commit 2f9e2f2

Browse files
committed
[Silver I] Title: 쿼드트리, Time: 8 ms, Memory: 69104 KB -BaekjoonHub
1 parent d8e699b commit 2f9e2f2

2 files changed

Lines changed: 25 additions & 46 deletions

File tree

백준/Silver/1992. 쿼드트리/README.md

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

55
### 성능 요약
66

7-
메모리: 79512 KB, 시간: 48 ms
7+
메모리: 69104 KB, 시간: 8 ms
88

99
### 분류
1010

1111
분할 정복, 재귀
1212

1313
### 제출 일자
1414

15-
2024년 5월 20일 13:28:08
15+
2025년 6월 26일 20:46:21
1616

1717
### 문제 설명
1818

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,28 @@
1-
import Foundation
2-
var wbPaperLength = Int(readLine()!)!
3-
var wbPaper = Array<[Bool]>()
4-
var answer = "("
5-
for _ in 0..<wbPaperLength{
6-
wbPaper.append(readLine()!.map{ (String($0) as NSString).boolValue })
7-
}
8-
func compress(paperLength: Int, StartX: Int, StartY:Int, paper: Array<[Bool]>)->String{
9-
var answer = ""
10-
var wNumber = 0
11-
for i in paper.indices where i >= StartY && i<StartY+paperLength{
12-
for j in paper.indices where j >= StartX && j<StartX+paperLength && paper[i][j] == false{
13-
wNumber += 1
1+
func isAllSame(fromRow: Int, fromCol: Int, size: Int) -> Bool {
2+
let first = image[fromRow][fromCol]
3+
4+
for i in fromRow..<fromRow+size {
5+
for j in fromCol..<fromCol+size {
6+
if first != image[i][j] { return false }
147
}
158
}
16-
if wNumber == paperLength*paperLength{
17-
answer += "0"
18-
return answer }
19-
if wNumber == 0 {
20-
answer += "1"
21-
return answer
22-
}
23-
24-
var topLeft = compress(paperLength: paperLength/2, StartX: StartX, StartY: StartY, paper: paper)
25-
if topLeft.count != 1{
26-
topLeft = "(" + topLeft + ")"
27-
}
28-
var topRight = compress(paperLength: paperLength/2, StartX: StartX+(paperLength/2), StartY: StartY, paper: paper)
29-
if topRight.count != 1{
30-
topRight = "(" + topRight + ")"
31-
}
32-
var bottomLeft = compress(paperLength: paperLength/2, StartX: StartX, StartY: StartY+(paperLength/2), paper: paper)
33-
if bottomLeft.count != 1{
34-
bottomLeft = "(" + bottomLeft + ")"
35-
}
36-
var bottomRight = compress(paperLength: paperLength/2, StartX: StartX+(paperLength/2), StartY: StartY+(paperLength/2), paper: paper)
37-
if bottomRight.count != 1{
38-
bottomRight = "(" + bottomRight + ")"
39-
}
40-
41-
return topLeft+topRight+bottomLeft+bottomRight
9+
return true
4210
}
43-
var wbNumber = compress(paperLength: wbPaperLength, StartX: 0, StartY: 0, paper: wbPaper)
44-
if wbNumber.count != 1{ wbNumber = "(" + wbNumber + ")" }
45-
print( wbNumber )
4611

47-
func +(left: (Int,Int), right: (Int,Int)) -> (Int,Int) {
48-
return (left.0 + right.0, left.1 + right.1)
12+
let n = Int(readLine()!)!
13+
let image = (0..<n).map { _ in readLine()!.map { String($0) }}
14+
15+
func compress(fromRow: Int, fromCol: Int, size: Int) -> String {
16+
if isAllSame(fromRow: fromRow, fromCol: fromCol, size: size) {
17+
return image[fromRow][fromCol]
18+
}
19+
let compressed = compress(fromRow: fromRow, fromCol: fromCol, size: size/2) +
20+
compress(fromRow: fromRow, fromCol: fromCol + size/2, size: size/2) +
21+
compress(fromRow: fromRow + size/2, fromCol: fromCol, size: size/2) +
22+
compress(fromRow: fromRow + size/2, fromCol: fromCol + size/2, size: size/2)
23+
24+
return "(\(compressed))"
4925
}
26+
27+
let result = compress(fromRow: 0, fromCol: 0, size: n)
28+
print(result)

0 commit comments

Comments
 (0)