ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํ”„๋ Œ์ฆˆ4๋ธ”๋ก(Swift)

2022. 10. 18. 20:29ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํ”„๋ Œ์ฆˆ4๋ธ”๋ก(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://school.programmers.co.kr/learn/courses/30/lessons/17679

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 ๋‚˜์˜ ํ’€์ด

 

์ง€๊ธˆ๊นŒ์ง€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๊ฐ€์žฅ ๊ธด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๊ฒƒ ๊ฐ™๋‹ค. 

๋งค๋ฒˆ ์ƒ๊ฐํ•˜์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ๊ธธ ์ˆ˜๋ก ์ฝ”๋“œ๋ฅผ ์ž˜ ์งœ์ง€ ๋ชปํ•œ๋‹ค๊ณ  ๋Š๋ผ๊ณ  ์ด๋ฒˆ์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋„ ๋ฌธ์ œ ๋ฐœ์ƒ -> ๋ฌธ์ œ ํ•ด๊ฒฐ -> ๋ฌธ์ œ ๋ฐœ์ƒ -> ๋ฌธ์ œํ•ด๊ฒฐ ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋ณตํ•จ์œผ๋กœ์จ ํ’€์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ข‹์ง€ ์•Š์€ ์ฝ”๋“œ๊ฐ€ ๋‚˜์™”๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

 

๊ทธ๋ž˜๋„ ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๊ธฐ ์ „์— ๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ๋ฅผ ๋ณต๊ธฐํ•ด๋ณธ๋‹ค.

 

checkํ•จ์ˆ˜๋Š” 2๊ฐ€์ง€ ์—ญํ• ์„ ํ•œ๋‹ค.

1. Boolํƒ€์ž…์˜ Flag๋ฅผ ์ „๋‹ฌํ•จ์œผ๋กœ์จ ๋ฉˆ์ถœ์ง€ ๊ณ„์†ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

2. Flag๊ฐ€ true๊ฐ€ ๋˜์–ด ๊ณ„์† ์ง„ํ–‰ํ•œ๋‹ค๋ฉด(4์นธ์ด ๋™์ผํ•œ ๊ฒŒ ์กด์žฌํ•œ๋‹ค๋ฉด) ์™ธ๋ถ€์˜ ๋ฐฐ์—ด arr์— check๋ฅผ ํ†ตํ•ด ์ง€์šด๋ฐฐ์—ด bor๋ฅผ ์ „๋‹ฌํ•ด์ค€๋‹ค.

 

ํ•ต์‹ฌ์€ i์™€ j๋ฅผ ๋Œ ๋•Œ m-1, n-1๊นŒ์ง€๋งŒ ๋„๋Š” ๊ฒƒ์ธ๋ฐ ์ด๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์ดˆ๊ณผ๋œ ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ๊ฒ€์‚ฌํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋ฉฐ answer์— ๋“ฑ๋ก๋œ ์ธ๋ฑ์Šค๋“ค์€ ์‹œ์ž‘๊ฐ’๋งŒ ์ €์žฅ์ด ๋˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋“ค์„ for๋ฌธ ๋Œ๋ฆฌ๋ฉด์„œ ์ฃผ๋ณ€์˜ 4์นธ์„ ๊ณต๋ฐฑ์œผ๋กœ ๋‘๋ฉด ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„๋“ค์„ ๊ณ ๋ คํ•ด์„œ ์ „๋ถ€ ์ง€์šธ ์ˆ˜ ์žˆ๋‹ค.

 

moveํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š”๋ฐ์—๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ ‘๊ทผ์„ ์ฐธ๊ณ ํ–ˆ๋‹ค.

์—ด์„ ํƒ์ƒ‰ํ•  ๋•Œ ์œ„์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉด ๊ณต๋ฐฑ์„ ๋ฐœ๊ฒฌํ•  ๋•Œ ๊ฐ’์„ ๋•ก๊ฒจ์ฃผ๊ณ  ๋˜ ์œ„์— ๊ฒƒ๋“ค๋„ ๋‹ค ๋•ก๊ฒจ์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Š” ์•„๋ž˜์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ˆ˜์›”ํ•˜๋‹ค.

 

์•„๋ž˜์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ณต๋ฐฑ์ผ ๋•Œ๋Š” emptyCount๋ฅผ 1์ฆ๊ฐ€์‹œํ‚ค๊ณ  ๊ณต๋ฐฑ์ด ์•„๋‹๋ฉด์„œ emptyCount๊ฐ€ 0์ด ์•„๋‹๋•Œ๋Š” ์ธ๋ฑ์Šค์— emptyCount๋ฅผ ๋”ํ•ด์„œ ๋Œ€์ฒดํ•ด์ฃผ๋ฉด ๋œ๋‹ค.  

 

์ด๊ฒƒ์ €๊ฒƒ true, false๋ฅผ ๋‚จ๋ฐœํ•˜๋ฉด์„œ ๋ฌธ์ œ๋Š” ํ’€์—ˆ๋‹ค๋Š” ์ ์—์„œ ์Šค์Šค๋กœ์—๊ฒŒ ์นญ์ฐฌ์„ ํ•˜์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๋‹ค๋Š” ์ ์—์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ๊ฐœ์„ ํ•  ์ ์„ ์ฐพ๋Š”๋‹ค.

 

func solution(_ m:Int, _ n:Int, _ board:[String]) -> Int {
    var arr = board.map{ $0.map{String($0) }}
    var cnt = 0
    func check(_ array: [[String]]) -> Bool {
        var Flag = false
        var bor = array
        var answer: [(Int, Int)] = []
        func isRemove(_ i: Int, _ j: Int) -> Bool {
            if bor[i][j] != "" && bor[i][j] == bor[i][j+1] && bor[i][j] == bor[i+1][j] && bor[i][j] == bor[i+1][j+1]{
                return true
            }
            return false
        }
        
        for i in 0..<m-1 {
            for j in 0..<n-1 {
                if isRemove(i, j) {
                    answer.append((i, j))
                }
            }
        }
        
        for (i, j) in answer {
            Flag = true
            bor[i][j] = ""
            bor[i][j+1] = ""
            bor[i+1][j] = ""
            bor[i+1][j+1] = ""
        }
        if Flag {
            arr = bor
            return true
        } else {
            return false
        }
    }
    
    func move(_ arr: [[String]]) -> [[String]] {
        var moveArr = arr
        for i in 0..<n {
            var emptyCount = 0
            for j in stride(from: m-1, to: -1, by: -1) {
                if moveArr[j][i] != "" {
                    if emptyCount != 0 {
                        moveArr[j+emptyCount][i] = moveArr[j][i]
                        moveArr[j][i] = ""
                    }
                } else {
                    emptyCount += 1
                }
            }
        }
        return moveArr
    }
    
    while true {
        if check(arr) {
            arr = move(arr)
        } else {
            break
        }
    }
    
    for i in 0..<m {
        for j in 0..<n {
            if arr[i][j] == "" {
                cnt += 1
            }
        }
    }
    
    return cnt
}

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

board์™€, temp ๋ฐฐ์—ด์„ ๋ถ„๋ฆฌ์‹œ์ผœ์„œ ์ค‘๋ณต์— ๋Œ€ํ•ด์„œ ์ฒ˜๋ฆฌํ•œ ์ ๊ณผ isChanged๋ฅผ ํ†ตํ•ด์„œ ๋ฐฐ์—ด์— 4์นธ์˜ ๊ฐ’์ด ์žˆ์„๋•Œ ํ•œ๋ฒˆ ๋” while๋ฌธ์„ ๋Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ ์ ์ด ์ธ์ƒ๊นŠ์—ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ํŒจํ„ด๋งค์นญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ธ๋ฑ์Šค ๋ฒ”์œ„์— ๋Œ€ํ•ด ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์—ˆ๊ณ , guard๋ฌธ์œผ๋กœ ์—ด๊ฑฐํ•จ์œผ๋กœ์จ ๋กœ์ง์„ ํ›จ์”ฌ ๋‹จ์ˆœํ™” ์‹œ์ผฐ๋‹ค.

 

import Foundation

// ์‚ฌ๊ฐํ˜• ์˜์—ญ์ด ๋ชจ๋‘ ๊ฐ™์€์ง€ ํ™•์ธ
func square(_ i: Int, _ j: Int, _ ch: String, _ m: Int, _ n: Int, _ board: [[String]]) -> Bool {
    let squareIndex = [(i, j), (i, j+1), (i+1, j), (i+1, j+1)]
    for (r, c) in squareIndex {
        guard (0..<m) ~= r, (0..<n) ~= c, ch == board[r][c] else { return false }
    }
    
    return true
}

// ๋นˆ ๋ธ”๋Ÿญ์„ ์ œ๊ฑฐ
func removeBlank(_ arr: [[String]], _ m: Int, _ n: Int) -> [[String]] {
    var temp = arr
    for i in 0..<n {
        var emptyCount = 0
        for j in stride(from: m-1, to: -1, by: -1) {
            if temp[j][i] != "-" {
                if emptyCount != 0 {
                    temp[j+emptyCount][i] = temp[j][i]
                    temp[j][i] = "-"
                }
            } else {
                emptyCount += 1
            }
        }
    }
    return temp
}

func solution(_ m:Int, _ n:Int, _ board:[String]) -> Int {
    var board = board.map{ $0.map{ String($0)} }
    var cnt = 0
    var isChanged = true
    
    while isChanged {
        var temp = board
        isChanged = false
        
        for i in 0..<m {
            for j in 0..<n where board[i][j] != "-" {
                if square(i, j, board[i][j], m, n, board) {
                    for (i, j) in [(i, j), (i, j+1), (i+1, j), (i+1, j+1)] {
                        if temp[i][j] != "-" {
                            cnt += 1
                            temp[i][j] = "-"
                        }
                    }
                    isChanged = true
                }
            }
        }
        
        board = removeBlank(temp, m, n)
    }
    
    return cnt
}