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