Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao - [1์ฐจ] ํ”„๋ Œ์ฆˆ4๋ธ”๋ก (Lv.2)

๊ฐ์ž ๐Ÿฅ” 2022. 9. 3. 17:01
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

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

 

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

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

programmers.co.kr

 

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

1. ์ž…๋ ฅ๋˜๋Š” board๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
2. 4๊ฐœ๊ฐœ๋กœ ๋ชจ์—ฌ์žˆ๋Š” ์ขŒํ‘œ๋ฅผ ํƒ์ƒ‰, removePoint (์ง€์šฐ๋Š”์ขŒํ‘œ) ์ง‘ํ•ฉ (set)์— ๋„ฃ์–ด์ฃผ๊ธฐ
3. removePoint์— ๊ฐ’๋“ค์ด ๋“ค์–ด์žˆ๋‹ค๋ฉด, ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ "-"๋กœ ๋ฐ”๊พธ์–ด ์‚ญ์ œ์ฒ˜๋ฆฌ & cnt ์ง€์šฐ๋Š” ๋ธ”๋ก ๊ฐฏ์ˆ˜ ๋”ํ•ด์ฃผ๊ธฐ
4. ์‚ญ์ œ๋œ ๋ถ€๋ถ„ ์œ„์—์žˆ๋Š” ๋ธ”๋ก๋“ค ๋‚ด๋ ค์ฃผ๊ธฐ

4๊ฐœ์˜ ๋ธ”๋ก๋งŒ ํ„ฐ๋œจ๋ฆฌ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋”ฐ๋กœ ์“ฐ์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ƒฅ ๊ตฌํ˜„์œผ๋กœ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์•„์„œ! ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ณด๋‹ˆ, ์ง€์šฐ๋Š” ์ขŒํ‘œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

์ฒซ๋ฒˆ์งธ ์ฒ˜๋Ÿผ A๊ฐ€ ์—ฌ์„ฏ๊ฐœ๊ฐ€ ์žˆ๋”๋ผ๋„, 4๊ฐœ์”ฉ ํƒ์ƒ‰ ํ›„, ์ค‘๋ณต๋˜๋Š” ์ขŒํ‘œ๋“ค์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ๊ฐ™์•˜๋‹ค. ๊ทธ๋ž˜์„œ '์ง€์šฐ๋Š” ์ขŒํ‘œ๋“ค'์„ Set์œผ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ์ฝ”๋“œ๋ฅด ์ž‘์„ฑํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ธ”๋ก์„ ๋‚ด๋ ค์ฃผ๋Š” ๋กœ์ง์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ๊ฐํ–ˆ๋‹ค. 

์•„ ๊ทธ๋ฆฌ๊ณ , ๋…ธ๋ž€์ƒ‰ ํ˜•๊ด‘ํŒฌ์œผ๋กœ ์น ํ•œ ๋ถ€๋ถ„๋งŒ ๋ด์ฃผ๋ฉด์„œ ๋ธ”๋ก์„ ์ฒ˜๋ฆฌํ–ˆ๋‹ค. (i+1์ด๋‚˜ j+1์ด ๋˜๋Š” ๋ถ€๋ถ„์€ ๊ตณ์ด for๋ฌธ ๋ฒ”์œ„ ๋‚ด์— ์•ˆ๋„ฃ์–ด์ค˜๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!

์ฒซ๋ฒˆ์งธ๋กœ ์ง  ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ๊ทผ๋ฐ test case 5๋ฒˆ๊ณผ 10๋ฒˆ์—์„œ ์‹คํŒจ๊ฐ€ ๋–ด๋‹ค. ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์•„๋‹ˆ๊ณ  ์‹คํŒจ์ธ ์ด์ƒ, ์–ด๋–ค case์—์„œ๋Š” ๋‚ด ๋กœ์ง์ด ์ž˜๋ชป๋๊ตฌ๋‚˜๋ฅผ ํŒŒ์•…ํ–ˆ๋‹ค.

๋”๋ณด๊ธฐ

์‹คํŒจ์ฝ”๋“œ

//
//  main.swift
//  Algorithm
//
//  Created by LeeJiSoo on 2022/09/03.
//

import Foundation

let m = 4
let n = 5
let board = ["CCBDE", "AAADE", "AAABF", "CCBBF"]
// ๊ฒฐ๊ณผ๊ฐ’ : 14

func solution(_ m:Int, _ n:Int, _ board:[String]) -> Int {

    // board๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
    var newBoard = splitBoard(board)

    // ์ง€์šฐ๋Š” ์ขŒํ‘œ๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ set ์„ค์ • & return ๊ฐ’ ์„ค์ •
    var removePoint = Set<[Int]>()
    var cnt = 0

    // ํ”„๋ Œ์ฆˆ 4๋ธ”๋ก์ด ์—†์–ด์งˆ๋•Œ๊นŒ์ง€ ๋Œ๋ ค์•ผํ•˜๋‹ˆ๊นŒ, while ๋ฌธ์œผ๋กœ removePoint ์กฐ๊ฑด ๊ฒ€์‚ฌ
    // ์›๋ž˜ while !removePoint.isEmpty ์„ ์กฐ๊ฑด์œผ๋กœ ์ฃผ์—ˆ๋Š”๋ฐ, removePoint๋Š” ๋งค๋ฒˆ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋‹ˆ๊นŒ ์ด ์กฐ๊ฑด์€ ์•ˆ๋˜๋Š”๊ตฌ๋‚˜๋ฅผ ๊นจ๋‹ณ์Œ
    while true {
        // 4๋ธ”๋ก ์œ„์น˜ ์ฐพ์•„์„œ removePoint์— ๋„ฃ์–ด์ฃผ๊ธฐ
        for i in 0..<m-1 {
            for j in 0..<n-1 {
                let point = newBoard[i][j]
                // ๋งŒ์•ฝ point ๊ฐ€ - ๋ผ๋ฉด, ์ด๋ฏธ ์ œ๊ฑฐ๋œ ์œ„์น˜์ด๋ฏ€๋กœ continue
                if point == "-" {
                    continue
                }
                // ์‚ฌ๋ฐฉ์„ ๋ณด๊ณ  ๋‹ค point ์™€ ๊ฐ™๋‹ค๋ฉด removePoint์— ํ•ด๋‹น ์ขŒํ‘œ ์ถ”๊ฐ€
                if newBoard[i][j+1] == point, newBoard[i+1][j] == point, newBoard[i+1][j+1] == point {
                    removePoint.insert([i, j])
                    removePoint.insert([i, j+1])
                    removePoint.insert([i+1, j])
                    removePoint.insert([i+1, j+1])
                }
            }
        }

        // removiPoint๊ฐ€ ์žˆ์œผ๋ฉด cnt ๋”ํ•ด์ฃผ๊ณ  ๋ธ”๋ก ์ง€์šฐ๊ณ , ์—†์œผ๋ฉด ๋ cnt ์ถœ๋ ฅ
        if !removePoint.isEmpty {
            cnt += removePoint.count
            for point in removePoint {
                newBoard[point[0]][point[1]] = "-"
            }
            removePoint = Set<[Int]>()
        } else {
            break
        }

        // ์ง€๊ธˆ ๋ธ”๋ก ์—†๋Š” ๊ณณ์ด "-"์ธ๋ฐ, ๋ธ”๋ก์„ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค์ค˜์•ผํ•จ
        for i in 0..<m-1 {
            for j in 0..<n {
                let block = newBoard[i][j]
                let under = newBoard[i+1][j]
                if under == "-" {
                    newBoard[i][j] = "-"
                    newBoard[i+1][j] = block
                } else {
                    continue
                }
            }
        }
    }
    return cnt
}


func splitBoard(_ board: [String]) -> [[String]] {
    var newBoard = [[String]]()
    for b in board  {
        let arr = b.map { String($0) }
        newBoard.append(arr)
    }
    return newBoard
}

print(solution(m, n, board))

์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋‹ˆ๊นŒ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋‹ค์–‘ํ•œ ์›์ธ์œผ๋กœ 5๋ฒˆ๊ณผ 10๋ฒˆ์„ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๊ณ  ์žˆ์—ˆ๋‹ค

1. ๊ณต๋ฐฑ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ๋ถ„
https://lsh424.tistory.com/85

๋‚˜๋Š”,, ๊ณต๋ฐฑ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์™„์ „ํžˆ ํ•ด์ฃผ์—ˆ๊ณ , ์ด ๋ธ”๋กœ๊ทธ์—์„œ ์•Œ๋ ค์ฃผ๋Š” ๋ฐ˜๋ก€๋ฅผ ๋Œ€์ž…ํ•ด๋ด๋„ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ–ˆ๋‹ค. ๋‚˜๋Š” ์ด ์›์ธ์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค.

2. ๋ฐ˜๋ก€ ํ†ต๊ณผํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ธฐ + ๋ฐฐ์—ด์„ ์žฌ๋ฐฐ์น˜(๋ธ”๋ก์„ ๋‚ด๋ฆด๋•Œ) ํ•  ๋•Œ ์ž˜๋ชป ํ–ˆ๋‹ค๊ณ  ํ•˜์‹œ๋Š” ๋ถ„
(๋ฐ˜๋ก€์ฐธ๊ณ )https://dev-note-97.tistory.com/105
(๋ธ”๋ก๋‚ด๋ฆด ๋•Œ ์˜ค๋ฅ˜๋ผ๊ณ  ํ•˜์‹œ๋Š” ๋ถ„, ์ฝ”๋“œ ์ฐธ๊ณ ) https://velog.io/@syi07030/Swift-%EA%B3%B5%EB%B6%80-Programmers-1%EC%B0%A8-%ED%94%84%EB%A0%8C%EC%A6%884%EB%B8%94%EB%A1%9D-level2-2018-KAKAO-BLIND-RECRUITMENT

์ด ๋ธ”๋กœ๊ทธ์—์„œ ์ œ์‹œํ•œ ๋ฐ˜๋ก€๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. 

// ๋ฐ˜๋ก€
let m = 5
let n = 6
let board = ["AAAAAA","BBAATB","BBAATB","JJJTAA","JJJTAA"]
// ๊ฒฐ๊ณผ๊ฐ’ : 24

๋Œ€๋ถ€๋ถ„ ๋ฐ˜๋ก€๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋Š” ์ด์œ ๋Š”, ๋ธ”๋ก์„ ๋‚ด๋ฆด๋•Œ ์ž˜๋ชป๋œ ๋กœ์ง์œผ๋กœ ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™์•˜๋‹ค. ๊ทธ๋ž˜์„œ ๋ธ”๋ก์„ ๋‚ด๋ ค์ฃผ๋Š” ๋ฐฉ์‹์„ ์ˆ˜์ •ํ•ด๋ณด๊ธฐ๋กœ ํ•œ๋‹ค.

๋„์ €ํžˆ ๋ฌด์Šจ ์›์ธ์ธ์ง€ ๋ชฐ๋ผ์„œ, ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์ฐพ์•„๋ดค๋‹ค. removePoint๋ฅผ ์‚ญ์ œํ•˜๊ณ , ๋ธ”๋ก์„ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค์ฃผ๋Š” ๊ณผ์ •์—์„œ ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ๋žŒ๋“ค์€ '๊ฑฐ๊พธ๋กœ' ์ง„ํ–‰ํ–ˆ๋‹ค. ์ฆ‰, ๋‚˜๋Š” ์œ„์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ for๋ฌธ์„ ๋Œ๋ ค์„œ, ๋ฐ‘์— "-"๋ฉด, ์œ„๋ž‘ ์•„๋ž˜๋ž‘ ๋ฐ”๊ฟ”๋ผ~ ์ด๋Ÿฐ ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์ด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ต์ด 24๊ฐ€ ๋‚˜์™€์•ผํ•˜๋Š” ๋ฐ˜๋ก€์—์„œ 20์œผ๋กœ ๋‚˜์˜จ๊ฒƒ์ด๊ณ , AA,TB๊ฐ€ ์ œ๋Œ€๋กœ ๋‚ด๋ ค์˜ค์ง€ ์•Š์•„์„œ ์‚ญ์ œ๋˜์ง€ ์•Š์€ 4๊ฐœ๊ฐ€ count ๋˜์ง€ ์•Š์•˜๋–ค ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์‚ฌ๋žŒ๋“ค์ด for๋ฌธ์„ ๋Œ๋ฆด๋•Œ, ๋ฐ‘๋ถ€๋ถ„ ๋ถ€ํ„ฐ ๋Œ๋ ธ๊ตฌ๋‚˜,, ๊ทธ๋ž˜์„œ ์ฐธ๊ณ ํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

// 5๋ฒˆ 10๋ฒˆ ์‹คํŒจ ์ฝ”๋“œ 
// ๋ธ”๋ก ๋‚ด๋ฆฌ๋Š” ๋ถ€๋ถ„์˜ ์ฝ”๋“œ
//        for i in 0..<m-1 {
//            for j in 0..<n {
//                let block = newBoard[i][j]
//                let under = newBoard[i+1][j]
//                if block != "-", under == "-" {
//                    newBoard[i+1][j] = block
//                    newBoard[i][j] = "-"
//                } else {
//                    continue
//                }
//            }
//        }


// ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”์•ผํ•œ๋‹ค.
for j in stride(from: 0, to: n, by: 1){ //์—ด
    var first = 0 //๋ธ”๋ก์ด ๋‚ด๋ ค์™€์•ผํ•  ํ–‰ ์œ„์น˜
    if newBoard[m-1][j] == "-" {
        first = m-1
    }

    for i in stride(from: m-2, through: 0, by: -1){ //ํ–‰
        if newBoard[i][j] == "-" {
            if i > first {
                first = i
            }
        } else if newBoard[i+1][j] == "-"{
            newBoard[first][j] = newBoard[i][j]
            newBoard[i][j] = "-"
            first -= 1
        }
    }
}

 

๋„๋Œ€์ฒด ์‹œํ—˜๋ณผ๋•Œ ์ด๋Ÿฐ ๋ฐ˜๋ก€๋ฅผ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๋ƒ” ๋ง์ด๋‹ค ใ… ใ… ใ…  ์•„์ง ์‹ค๋ ฅ์ด ๋งŽ์ด ๋ถ€์กฑํ•œ๊ฐ€๋ณด๋‹ค. ์–ด๋””์„œ ์‹คํŒจ๊ฐ€ ๋–ณ๋Š”์ง€ ์ „ํ˜€ ๊ฐ์„ ์žก์ง€ ๋ชปํ•ด์„œ ์—ฌ๋Ÿฌ ์ธํ„ฐ๋„ท์„ ์ฐธ๊ณ ํ•ด๋ดค๋‹ค. ๋‹ค๋“ค ์™œ ์ฒœ์žฌ๊ฐ™์ง€?

 

์ฆ‰, ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€, ๋ธ”๋ก์„ ์•„๋ž˜๋กœ ๋‚ด๋ฆฌ๋Š” ๊ณผ์ • -> ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, ๋ฐ‘์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ด์„œ ์ฐจ๊ทผ์ฐจ๊ทผ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ๋‚ด๋ ค์ฃผ๋Š”๊ฒŒ ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค!!

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%ED%94%84%EB%A0%8C%EC%A6%884%EB%B8%94%EB%A1%9D/main.swift

 

GitHub - deslog/Algorithm: โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ

โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ. Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

 

import Foundation

//let m = 4
//let n = 5
//let board = ["CCBDE", "AAADE", "AAABF", "CCBBF"]
// ๊ฒฐ๊ณผ๊ฐ’ : 14

// ๋‹ค๋ฅธ test case
//let m = 8
//let n = 2
//let board = ["CC", "BB", "AA", "BB", "BB", "AA", "BB", "CC"]
// ๊ฒฐ๊ณผ๊ฐ’ : 16

// ๋ฐ˜๋ก€
let m = 5
let n = 6
let board = ["AAAAAA","BBAATB","BBAATB","JJJTAA","JJJTAA"]
// ๊ฒฐ๊ณผ๊ฐ’ : 24

func solution(_ m:Int, _ n:Int, _ board:[String]) -> Int {

    // board๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
    var newBoard = splitBoard(board)

    // ์ง€์šฐ๋Š” ์ขŒํ‘œ๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ set ์„ค์ • & return ๊ฐ’ ์„ค์ •
    var removePoint = Set<[Int]>()
    var cnt = 0

    // ํ”„๋ Œ์ฆˆ 4๋ธ”๋ก์ด ์—†์–ด์งˆ๋•Œ๊นŒ์ง€ ๋Œ๋ ค์•ผํ•˜๋‹ˆ๊นŒ, while ๋ฌธ์œผ๋กœ removePoint ์กฐ๊ฑด ๊ฒ€์‚ฌ
    // ์›๋ž˜ while !removePoint.isEmpty ์„ ์กฐ๊ฑด์œผ๋กœ ์ฃผ์—ˆ๋Š”๋ฐ, removePoint๋Š” ๋งค๋ฒˆ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋‹ˆ๊นŒ ์ด ์กฐ๊ฑด์€ ์•ˆ๋˜๋Š”๊ตฌ๋‚˜๋ฅผ ๊นจ๋‹ณ์Œ
    while true {
        // 4๋ธ”๋ก ์œ„์น˜ ์ฐพ์•„์„œ removePoint์— ๋„ฃ์–ด์ฃผ๊ธฐ
        for i in 0..<m-1 {
            for j in 0..<n-1 {
                let point = newBoard[i][j]
                // ๋งŒ์•ฝ point ๊ฐ€ - ๋ผ๋ฉด, ์ด๋ฏธ ์ œ๊ฑฐ๋œ ์œ„์น˜์ด๋ฏ€๋กœ continue
                if point == "-" {
                    continue
                }
                // ์‚ฌ๋ฐฉ์„ ๋ณด๊ณ  ๋‹ค point ์™€ ๊ฐ™๋‹ค๋ฉด removePoint์— ํ•ด๋‹น ์ขŒํ‘œ ์ถ”๊ฐ€
                if newBoard[i][j+1] == point, newBoard[i+1][j] == point, newBoard[i+1][j+1] == point {
                    removePoint.insert([i, j])
                    removePoint.insert([i, j+1])
                    removePoint.insert([i+1, j])
                    removePoint.insert([i+1, j+1])
                }
            }
        }

        // removiPoint๊ฐ€ ์žˆ์œผ๋ฉด cnt ๋”ํ•ด์ฃผ๊ณ  ๋ธ”๋ก ์ง€์šฐ๊ณ , ์—†์œผ๋ฉด ๋ cnt ์ถœ๋ ฅ
        if !removePoint.isEmpty {
            cnt += removePoint.count
            for point in removePoint {
                newBoard[point[0]][point[1]] = "-"
            }
            removePoint = Set<[Int]>()
        } else {
            break
        }

        // ์ง€๊ธˆ ๋ธ”๋ก ์—†๋Š” ๊ณณ์ด "-"์ธ๋ฐ, ๋ธ”๋ก์„ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค์ค˜์•ผํ•จ
        // ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๋ธ”๋ก์„ ์ œ๋Œ€๋กœ ๋ฐ‘์œผ๋กœ ๋ชป๋‚ด๋ ค์ฃผ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด์„œ ์•„๋ž˜ ์ฝ”๋“œ๋กœ ์ˆ˜์ •!
        //        for i in 0..<m-1 {
        //            for j in 0..<n {
        //                let block = newBoard[i][j]
        //                let under = newBoard[i+1][j]
        //                if block != "-", under == "-" {
        //                    newBoard[i+1][j] = block
        //                    newBoard[i][j] = "-"
        //                } else {
        //                    continue
        //                }
        //            }
        //        }
        
        // ์ด๋ ‡๊ฒŒ ๋ฐ‘์—์„œ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์˜ฌ๋ผ์˜ค๋ฉด์„œ ๋ธ”๋ก์„ ๋‚ด๋ ค์ค˜์•ผํ•จ
        for j in stride(from: 0, to: n, by: 1) { //์—ด
            var first = 0 //๋ธ”๋ก์ด ๋‚ด๋ ค์™€์•ผํ•  ํ–‰ ์œ„์น˜
            if newBoard[m-1][j] == "-" {
                first = m-1
            }

            for i in stride(from: m-2, through: 0, by: -1) { //ํ–‰
                if newBoard[i][j] == "-" {
                    if i > first {
                        first = i
                    }
                } else if newBoard[i+1][j] == "-"{
                    newBoard[first][j] = newBoard[i][j]
                    newBoard[i][j] = "-"
                    first -= 1
                }
            }
        }
    }
    return cnt
}


func splitBoard(_ board: [String]) -> [[String]] {
    var newBoard = [[String]]()
    for b in board  {
        let arr = b.map { String($0) }
        newBoard.append(arr)
    }
    return newBoard
}

print(solution(m, n, board))

 

 

๋ฐ˜์‘ํ˜•