Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 프렌즈4블록 #71

Closed
hwangJi-dev opened this issue Jan 18, 2023 · 0 comments
Closed

[Algorithm] 프렌즈4블록 #71

hwangJi-dev opened this issue Jan 18, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 18, 2023

💬 문제

[코딩테스트 연습 - [1차] 프렌즈4블록](https://school.programmers.co.kr/learn/courses/30/lessons/17679)


💬 Idea

  1. board를 세로형 2차원 배열로 바꾼다.
  2. 지워지는 좌표를 (4블록)탐색해서 point에 넣어준다.
  3. removePoint에 값들이 들어있다면 해당 좌표를 “-”로 바꾸고, result에 개수를 더해준다.
  4. “-”로 표시된 블록(지워진 블록) 위에 있는 블록들을 내린다.

💬 풀이

func solution(_ m:Int, _ n:Int, _ board:[String]) -> Int {
    var boardArr: [[String]] = Array(repeating: Array(repeating: "", count: m), count: n)
    var result = 0
    var isBreak = Array(repeating: false, count: m)
    var point: Set<[Int]> = []
    
    // 세로형 2차원 배열로 바꾸기
    for i in 0...board.count - 1 {
        let k = Array(board.reversed()[i]).map({ String($0) })
        for j in 0...k.count - 1 {
            boardArr[j][i] = k[j]
        }
    }
    
    isBreak[0] = true
    
    while isBreak.contains(true) {
        for i in 0...m - 2 {
            var breakArr = Array(repeating: false, count: n)
            for j in 0...n - 2 {
                if boardArr[j][i] == boardArr[j][i + 1] && boardArr[j][i + 1] == boardArr[j + 1][i + 1] && boardArr[j][i + 1] == boardArr[j + 1][i] && boardArr[j][i] != "" && boardArr[j][i].allSatisfy({ $0.isUppercase }) {
                    point.insert([j, i])
                    point.insert([j, i + 1])
                    point.insert([j + 1, i + 1])
                    point.insert([j + 1, i])
                    breakArr[j] = true
                } else {
                    breakArr[j] = false
                }
            }
            
            if breakArr.contains(true) {
                isBreak[i] = true
            } else {
                isBreak[i] = false
            }
        }
        
	// point에 들어간 좌표들 "-"로 바꾸기
        if !point.isEmpty {
            for k in point.sorted(by: { $0[0] < $1[0] }) {
                boardArr[k[0]][k[1]] = "-"
            }
        }
        
	// "-"로 바꾼 부분 삭제하고 삭제된만큼 ""로 채우기
        for (index, l) in boardArr.enumerated() {
            boardArr[index] = l.filter({ $0 != "-" })
            if boardArr[index].count < m {
                for _ in 1...m - boardArr[index].count {
                    boardArr[index].append("")
                }
            }
        }
        
        result += point.count
        point.removeAll()
    }
    
    return result
}

print(solution(4,5,  ["CCBDE", "AAADE", "AAABF", "CCBBF"]))
print(solution(6,6,  ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"]))
print(solution(6, 6, ["AABBEE","AAAEEE","VAAEEV","AABBEE","AACCEE","VVCCEE" ]))
print(solution(2, 2, ["aa", "aa"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant