๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Programmers] ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ

๋ฌธ์ œ๋งํฌ ๐Ÿ‘‰ https://programmers.co.kr/learn/courses/30/lessons/81302?language=swift

 

๋ฌธ์ œ ์ดํ•ด 

๋ฉด์ ‘์— ์˜จ ์‚ฌ๋žŒ๋“ค์ด ๊ฑฐ๋ฆฌ๋ฅผ ๋‘๊ณ  ์•‰์•˜๋Š” ์ง€ ์œ ๋ฌด๋ฅผ ๋ฌป๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ฑฐ๋ฆฌ๋ฅผ ์ž˜ ๋‘์—ˆ๋Š” ์ง€ ํŒ๋‹จํ•˜๋Š” ๊ธฐ์ค€์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

- ์ž๋ฆฌ ์‚ฌ์ด์— ํŒŒํ‹ฐ์…˜์ด ์กด์žฌํ•˜๊ฑฐ๋‚˜ ์ž๋ฆฌ๊ฐ€ ๋Œ€๊ฐ์„ ์ผ ๊ฒฝ์šฐ O 

- ๊ฑฐ๋ฆฌ๊ฐ€ 2์ธ๋ฐ ์ž๋ฆฌ ์‚ฌ์ด์— ์ฑ…์ƒ์ด ๋†“์—ฌ์ ธ ์žˆ๋‹ค๋ฉด X

๊ฑฐ๋ฆฌ๋‘๊ธฐ๊ฐ€ ์ž˜ ์ง€์ผœ์ง„ ๋ฉด์ ‘์žฅ์€ 1๋กœ, ์ง€์ผœ์ง€์ง€ ์•Š๋‹ค๋ฉด 0์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 

๋ฌธ์ œ ํ’€์ด

dfs ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 

์‘์‹œ์ž๊ฐ€ ์•‰์•„์žˆ๋Š” ์ž๋ฆฌ P ์ผ๊ฒฝ์šฐ dfs๋ฅผ ๋Œ๋ฉด์„œ ์˜†์— ์นธ๋ง‰์ด๋ฉด ํŒจ์“ฐ, ์ฑ…์ƒ์ด๋ฉด ํ•œ๋ฒˆ ๋” dfs ๋Œ์•„์„œ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ๋” ๋Œ ๊ฒฝ์šฐ, ์ด์ „์— ํ™•์ธํ–ˆ๋˜ ์ž๋ฆฌ๋Š” ํ™•์ธํ•˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ ํ‰์†Œ์—๋Š” visited ๋ฐฐ์—ด์„ ๋‘์—ˆ์ง€๋งŒ, ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ตœ๋Œ€ 2๋ฒˆ๋งŒ dfsํƒ์ƒ‰์„ ํ•˜๋ฏ€๋กœ ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. 

 

 

์œ ์šฉํ•œ ์ •๋ณด -> String์„ Array๋กœ ๋ฐ”๊พธ๊ธฐ

- solutionํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ places๋Š” "POOXP" ํ˜•ํƒœ๋กœ ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ["P","O","O","X","P"] ๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. 

- ๋ฐฉ๋ฒ• 1. Array(string) 

- ๋ฐฉ๋ฒ• 2. let array = string.map { String($0) }

 

๋ฐฉ๋ฒ• 2๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ  ๋งํฌ

 

์ฝ”๋“œ 

import Foundation

var direction: [(Int,Int)] = [(-1,0),(0,1),(1,0),(0,-1)] // ์œ„, ์˜ค๋ฅธ์ชฝ, ์•„๋ž˜, ์™ผ์ชฝ

func dfs(_ places: [[String]], _ xy: (Int,Int), _ count: Int, _ from: (Int, Int)) -> Bool {
    if count == 2 {
        if places[xy.0][xy.1] == "P" {
            return false
        } else {
            return true
        }
    }
    for dir in direction {
        let nextDirection: (Int,Int) = (xy.0 + dir.0, xy.1 + dir.1)
        if nextDirection.0 == from.0 && nextDirection.1 == from.1 { continue } // ์ด์ „์— ์ฐพ์€ ๊ณณ
        if nextDirection.0 < 0 || nextDirection.0 >= 5 { continue } // ๋ฒ”์œ„ ๋ฐ–
        if nextDirection.1 < 0 || nextDirection.1 >= 5 { continue } // ๋ฒ”์œ„ ๋ฐ–
        if places[nextDirection.0][nextDirection.1] == "X" { continue } // ๊ฐ€๋ฆผ๋ง‰
        if places[nextDirection.0][nextDirection.1] == "P" { return false }
        if places[nextDirection.0][nextDirection.1] == "O" {
            if dfs(places, nextDirection, count+1, (xy.0, xy.1)) == false {
                return false
            } else {
                continue
            }
        }
    }
    return true
}

func solution(_ places:[[String]]) -> [Int] {
    var result = [Int]()
    places.forEach {
        var places: [[String]] = []
        for oneLine in $0 {
            let arr = oneLine.map { String($0) }
            places.append(arr)
        }
        
        print(places)
        var flag = 0
        
        for (xIndex, x) in places.enumerated() {
            for (yIndex, y) in x.enumerated() {
                if y == "P" {
                    print(xIndex,yIndex)
                    if dfs(places, (xIndex,yIndex), 0, (0,0)) == false {
                        flag = 1
                        break
                    }
                }
            }
            if flag == 1 {
                break
            }
        }

        if flag == 0 {
            result.append(1)
        } else {
            result.append(0)
        }
    }
    return result
}