ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์•ˆ์ „์ง€๋Œ€
    ์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2023. 6. 29. 23:36

    ๋‹ค๋ฅธ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋ฃจ์— ํ•œ ๋ฌธ์ œ์”ฉ ํ’€๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์•ˆ์ „์ง€๋Œ€ ๋ฌธ์ œ๋ฅผ ํ’€๊ฒŒ ๋˜์—ˆ๊ณ  ๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋‹ˆ, 2์ฐจ์› ๋ฐฐ์—ด์—์„œ ํญํƒ„์ด ์„ค์น˜๋œ ์œ„์น˜ ์ฃผ๋ณ€์— ํญํƒ„์„ ์„ค์น˜ํ•˜๊ณ , ํญํƒ„์ด ์„ค์น˜๋˜์ง€ ์•Š์€ ์ง€์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์œ ํ˜•์˜ ๋ฌธ์ œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ํ’€์–ด๋ด์„œ ์ต์ˆ™ํ•ด์ ธ ์žˆ์–ด์„œ ๋„์ „ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.์ฒ˜์Œ์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋กœ๋Š” ์ฃผ์–ด์ง„ ๊ธฐ๋ณธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ†ต๊ณผํ–ˆ์ง€๋งŒ, ์ œ์ถœํ•  ๋•Œ๋Š” ์ ˆ๋ฐ˜ ์ด์ƒ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์‹คํŒจํ•˜์—ฌ ์–ด๋ฆฌ๋‘ฅ์ ˆํ–ˆ์Šต๋‹ˆ๋‹ค...??? ๐Ÿ˜ฎ

     

    ์–ด๋ผ๋ผ..? ๐Ÿ˜ญ


    1. ๋ฌธ์ œ ๋งํฌ 

     

     

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

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

    programmers.co.kr

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

     

    2. ์ฝ”๋“œ

    func solution(_ board:[[Int]]) -> Int {
        let row = [-1,-1,-1,0,0,1,1,1]
        let col = [-1,0,1,-1,1,-1,0,1]
        let size = board.count
        var tempArr = board
    
        func setBombArea(i:Int, j:Int){
            for k in 0..<8{
                if (i + row[k] >= 0 && i + row[k] < size) && (j + col[k] >= 0 && j + col[k] < size){
                    tempArr[i+row[k]][j+col[k]] = 1
                }
            }
    
        }
        // ํญํƒ„ ์ฐพ๊ณ  ์„ค์น˜ํ•˜๊ธฐ ๋ถ€๋ถ„
        for i in 0..<size{
            for j in 0..<size{
                if board[i][j] != 1{
                    continue
                }
                setBombArea(i: i, j: j)
            }
        }
        let safeArea = tempArr.map({ $0.filter({ $0 == 0 }).count }).reduce(0,+)
        return safeArea
    }

     

    3.  ์ฝ”๋“œ ์„ค๋ช…

    ๋จผ์ € row, col ๋ณ€์ˆ˜๋Š” ์œ„, ์•„๋ž˜, ์–‘ ์˜†, ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ณต๋ฌธ์—์„œ ์ธ๋ฑ์Šค ๊ฐ’์— ๋”ํ•ด ํ•ด๋‹น ๋ฐฉํ–ฅ์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    size ๋ณ€์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด board์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์œผ๋กœ "board๋Š” n * n ํฌ๊ธฐ์˜ ๋ฐฐ์—ด"์ด๋ผ๊ณ  ๋ฌธ์ œ์— ๋ช…์‹œ๋˜์–ด ์žˆ์–ด ๊ณ ์ •๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

    ์ด์ค‘ for-in๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๊ฐ’์ด 1์ด ์•„๋‹ˆ๋ผ๋ฉด continue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ์ˆœ์„œ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๊ณ  ๊ฐ’์ด 1์ด๋ผ๋ฉด setBombArea ๋ฉ”์„œ๋“œ์— ํ˜„์žฌ ์ธ๋ฑ์Šค ์œ„์น˜ i, j๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์œ„, ์•„๋ž˜, ์–‘ ์˜†, ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์„ ํƒ์ƒ‰ํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํƒ์ƒ‰ํ•  ๋•Œ๋Š” ๊ฐ ๋ฐฉํ–ฅ์œผ๋กœ ๋”ํ•ด์ง„ ๊ฐ’๋“ค์ด board์˜ ๋ฐฐ์—ด ๋ฒ”์œ„ ๋‚ด์— ์†ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

    ๋งŒ์•ฝ ํ•ด๋‹น ์œ„์น˜๊ฐ€ ๋ฐฐ์—ด ๋ฒ”์œ„ ๋‚ด์— ์†ํ•œ๋‹ค๋ฉด tempArr์— 1์„ ์„ค์ •ํ•˜์—ฌ ํญํƒ„์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ tempArr์—์„œ ๊ฐ’์ด 0์ธ ์›์†Œ๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด safeArea์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

     

     


    ๋ฌธ์ œ๋ฅผ ๋งžํžˆ๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ ๊ณผ์ •์—์„œ ๋‘ ๊ฐ€์ง€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฒซ์งธ๋กœ ๋ฐฐ์—ด ๋ฒ”์œ„๋ฅผ ์ž˜๋ชป ๊ณ ๋ คํ•˜์—ฌ 0์„ ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘˜์งธ๋กœ ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ณ€์ˆ˜๋ช…์ด ์ค‘๋ณต๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ‹€๋ ธ์„ ๋•Œ ๋‹น์‹œ์— ์„ค๋งˆ ๋‚ด๊ฐ€ ๋ฌธ์ œ๋ฅผ ์ž˜๋ชป ์ดํ•ดํ–ˆ์„๊นŒ?? ๋‹ค์‹œ ์ฝ์–ด๋ณด๊ณ  ๋‚ด๊ฐ€ ๋ฐœ๊ฒฌํ•˜์ง€ ๋ชปํ•˜๋Š” ๋‹ค๋ฅธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ์žˆ์„๊นŒ ์ƒ๊ฐํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹;;๐Ÿ˜…

     

     

     

Designed by Tistory.