๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ 3 ๋น›์˜ ๊ฒฝ๋กœ ์‚ฌ์ดํด

by Fomagran ๐Ÿ’ป 2021. 9. 24.
728x90
๋ฐ˜์‘ํ˜•

 

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋น›์˜ ๊ฒฝ๋กœ ์‚ฌ์ดํด

๊ฐ ์นธ๋งˆ๋‹ค S, L, ๋˜๋Š” R๊ฐ€ ์จ์ ธ ์žˆ๋Š” ๊ฒฉ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ์ด ๊ฒฉ์ž์—์„œ ๋น›์„ ์˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฉ์ž์˜ ๊ฐ ์นธ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ดํ•œ ์„ฑ์งˆ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋น›์ด "S"๊ฐ€ ์จ์ง„ ์นธ์— ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ, ์ง์ง„

programmers.co.kr


Solution

 

1. ์‚ฌ์ดํด์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ดํ•ดํ•œ๋‹ค.

 

์‚ฌ์ดํด์€ ์ฒ˜์Œ ์‹œ์ž‘ํ•œ ์นธ์—์„œ ์ฒ˜์Œ ์‹œ์ž‘ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋‹ค์‹œ ๋˜๋Œ์•„ ์˜ฌ ๋•Œ ์‚ฌ์ดํด์ด ํ˜•์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

์ฆ‰ (0,0)์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋น›์„ ์ˆ๊ณ  ๋‹ค์‹œ (0,0)์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋น›์ด ์žˆ๋‹ค๋ฉด ์‚ฌ์ดํด์ž…๋‹ˆ๋‹ค.

 

2. ์œ„์น˜์™€ ๋ฐฉํ–ฅ์„ ๊ธฐ์–ตํ•  Location ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

struct Location:Equatable {
    var x:Int,y:Int,d:String
}

 

3. ์‚ฌ์ดํด์„ ๊ตฌํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด๋†“๋Š”๋‹ค.

 

dValue: ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ์–ด์•ผํ•  ๊ฐ’

 

next: ์ง„ํ–‰๋œ ๋ฐฉํ–ฅ์ด ์šฐํšŒ์ „ ์ขŒํšŒ์ „ ์ง์ง„์— ๋”ฐ๋ผ ๋ฐ”๋€” ๋ฐฉํ–ฅ

 

answer: ์‚ฌ์ดํด์˜ ๊ธธ์ด๋“ค

 

g: grid์˜ ์ด์ค‘ ๋ฐฐ์—ด

 

visited: ๊ฐ ์นธ์˜ ๋ฐฉํ–ฅ๋ณ„ ๋ฐฉ๋ฌธ์ด๋ ฅ์„ ์ €์žฅ

 

let dValue = ["up":(0,-1),"down":(0,1),"left":(-1,0),"right":(1,0)]
let next:[String:[String:String]] = ["L":["down":"right","right":"up","up":"left","left":"down"],"R":["down":"left","right":"down","up":"right","left":"up"],"S":["down":"down","right":"right","up":"up","left":"left"]]
var answer:[Int] = []
let g = grid.map{$0.map{String($0)}}
var visited = Array(repeating: Array(repeating:["up":false,"down":false,"right":false,"left":false], count: g[0].count), count: g.count)

 

4. ๋ชจ๋“  ์นธ์—์„œ ๋ชจ๋“  ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ์‚ฌ์ดํด์„ ๋งŒ๋“œ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

 

3์ค‘ ํฌ๋ฌธ ๋ชจ๋“  x,y์—์„œ์˜ ์œ„,์•„๋ž˜,์˜ค๋ฅธ์ชฝ,์™ผ์ชฝ์œผ๋กœ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด break๋ฅผ ํ•˜๊ณ  ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค์Œ ์œ„์น˜๋กœ ๋„˜์–ด๊ฐ€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋งจ ์ฒ˜์Œ ์‹œ์ž‘๋œ ์œ„์น˜๊ฐ€ ๋˜๋Œ์•„์™”๋‹ค๋ฉด count๋ฅผ answer์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

 for y in 0..<g.count {
        for x in 0..<g[0].count {
            for d in dValue.keys {
                let first = Location(x: x, y: y, d: d)
                var location:Location = first
                var count:Int = 0
                while true {
                    if visited[location.y][location.x][location.d]! { break }
                    visited[location.y][location.x][location.d] = true
                    count += 1
                    let direction = g[location.y][location.x]
                    location.d = next[direction]![location.d]!
                    location.x += dValue[location.d]!.0
                    location.y += dValue[location.d]!.1
                    location.x = location.x < 0 ? g[0].count-1 : location.x%g[0].count
                    location.y = location.y < 0 ? g.count-1 : location.y%g.count
                    if location == first && count != 0{
                        answer.append(count)
                        break
                    }
                }
            }
        }
    }

 

5. answer๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

return answer.sorted()

Source Code

 


P.S

 

์ด๊ฑด 2๋‹จ๊ณ„๋ผ๊ณ  ๋ณด๊ธฐ์—” ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™๋‹ค.... 3๋‹จ๊ณ„์—์„œ๋„ ์กฐ๊ธˆ ๋‚œ์ด๋„ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ™์€๋ฐ...

 

์ฒ˜์Œ์— ์‚ฌ์ดํด์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ดํ•ดํ•  ๋•Œ 1 - 2 - 3 - 4 ์™€ 5 - 1 - 2 - 3 - 4 ๋Š” ๋‹ค๋ฅธ ์‚ฌ์ดํด์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ 

 

๋ฐฉ๋ฌธ์ด๋ ฅ์„ ๋น›์ด ์ฒ˜์Œ ์‹œ์ž‘ํ•  ๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”ํ•ด์คฌ๋Š”๋ฐ.. ๊ทธ๊ฒŒ ์•„๋‹ˆ์˜€๋‹ค... ๊ทธ๊ฒƒ๋งŒ ๋ฐ”๋กœ ์•Œ์•˜๋”๋ผ๋„ ๋นจ๋ฆฌ ํ’€์—ˆ์„ํ…๋ฐ..

 

Swift๋กœ๋Š” 2๋ฒˆ์งธ๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ผ ์‚ฌ๋žŒ์ด์—ฌ์„œ  ๋ฟŒ๋“ฏํ–ˆ๋‹ค...ใ…Žใ…Ž

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€