관리 메뉴

나만을 위한 블로그

[Algorithm] 프로그래머스 - 안전지대 (Kotlin) 본문

알고리즘 문제 풀이/프로그래머스

[Algorithm] 프로그래머스 - 안전지대 (Kotlin)

참깨빵위에참깨빵_ 2023. 1. 13. 16:33
728x90
반응형
지뢰는 2차원 배열 board에 1로 표시돼 있고 board에는 지뢰가 매설된 지역 1, 지뢰가 없는 지역 0만 존재한다. 지뢰가 매설된 지도 board가 매개변수로 주어질 때, 안전한 지역의 칸 수를 리턴하는 solution()을 완성하라

 

 

사람들은 이런 문제를 어떻게 푸는 건지 모르겠다. 절망감 1스택과 함께 인터넷의 풀이를 찾아봤다.

 

https://velog.io/@as9587/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%BD%94%EB%94%A9%ED%85%8C%EC%8A%A4%ED%8A%B8-%EC%9E%85%EB%AC%B8-%EC%95%88%EC%A0%84%EC%A7%80%EB%8C%80-JAVA-22%EB%85%84-10%EC%9B%94-1%EC%9D%BC

 

프로그래머스 코딩테스트 입문 안전지대 [JAVA] - 22년 10월 1일

 

velog.io

class Solution {
    fun solution(board: Array<IntArray>): Int {
        var answer = 0
        val boomExplosionRange = 1
        val copyBoard = Array(board.size) { IntArray(board[0].size) }
        var tempIndex = 0
        for (rows in board) {
            copyBoard[tempIndex] = rows.copyOf(board[tempIndex].size)
            tempIndex++
        }
        for (i in board.indices) {
            for (j in 0 until board[i].size) {
                val `val` = board[i][j]
                if (`val` == 0) continue
                makeBoomArea(i, j, boomExplosionRange, copyBoard)
            }
        }
        for (ints in copyBoard) {
            for (anInt in ints) {
                if (anInt == 0) answer++
            }
        }
        return answer
    }

    private fun makeBoomArea(row: Int, col: Int, boomExplosionRange: Int, board: Array<IntArray>) {
        for (r in row - boomExplosionRange..row + boomExplosionRange) {
            if (r < 0 || r >= board.size) continue
            for (c in col - boomExplosionRange..col + boomExplosionRange) {
                if (c < 0 || c >= board[0].size) continue
                board[r][c] = 1
            }
        }
    }
}

 

아래는 또 다른 풀이다.

 

class Solution {
    fun solution(board: Array<IntArray>): Int {
        var answer = 0
        val wrapBoard = Array(board.size + 2) { IntArray(board[0].size + 2) }

        for (i in wrapBoard.indices) {
            for (j in wrapBoard[i].indices) {
                if (i == 0 || j == 0 || i == wrapBoard.size - 1 || j == wrapBoard[0].size - 1) {
                    wrapBoard[i][j] = 1
                }
                if (i > 0 && i < wrapBoard.size - 1 && j > 0 && j < wrapBoard[0].size - 1) {
                    if (board[i - 1][j - 1] == 1) {
                        wrapBoard[i][j] = 1
                        wrapBoard[i - 1][j - 1] = 1
                        wrapBoard[i - 1][j] = 1
                        wrapBoard[i - 1][j + 1] = 1
                        wrapBoard[i][j - 1] = 1
                        wrapBoard[i][j + 1] = 1
                        wrapBoard[i + 1][j - 1] = 1
                        wrapBoard[i + 1][j] = 1
                        wrapBoard[i + 1][j + 1] = 1
                    }
                }
            }
        }
        for (array in wrapBoard) {
            for (e in array) {
                if (e == 0) {
                    answer++
                }
            }
        }
        return answer
    }
}

 

반응형
Comments