ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฐฉ๋ฌธ ๊ธธ์ด(Swift)

2022. 10. 20. 16:53ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฐฉ๋ฌธ ๊ธธ์ด(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

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

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

programmers.co.kr

 

 ๋‚˜์˜ ํ’€์ด

 

์ฒ˜์Œ์—๋Š” ์ถœ๋ฐœํ•œ ์ง€์ ์—์„œ ๋„์ฐฉํ•œ ์ง€์ ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ [(Int, Int): [(Int, (Int)]]ํ˜•์‹์˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์„ ์–ธํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŠœํ”Œ์€ Hashableํ•˜์ง€ ์•Š์•„์„œ๋˜์ง€ ์•Š์•˜๊ธฐ์— ๋ฌธ์ž์—ด: ๋ฌธ์ž์—ด๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ๋‘์—ˆ๋‹ค. 

 

์ขŒํ‘œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ณ  ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅํ•˜๋ฉด ์ƒ๋‹นํžˆ ์ง€์ €๋ถ„ํ•œ ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•˜๋‹ˆ ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์ขŒํ‘œ๋“ค์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๊ธฐ์กด์ขŒํ‘œ์˜ ์ถœ๋ฐœ์ ๊ณผ ๋„์ฐฉ์ ์ด ์ƒˆ๋กœ์šด ์ขŒํ‘œ์˜ ์ถœ๋ฐœ์ ๊ณผ ๋„์ฐฉ์ ์ด ๊ฐ™์œผ๋ฉด ์นด์šดํŠธํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ๊ณผ ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋„ ์นด์šดํŠธํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ฆ‰ (5, 5)์˜ ์ขŒํ‘œ์—์„œ (5, 4)๋กœ ์œ„๋กœ ํ•œ๋ฒˆ ์ด๋™ํ•˜๋Š” ๊ฒƒ๊ณผ (5, 4)์ขŒํ‘œ์—์„œ (5, 5)๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์€ ์ค‘๋ณตํ•ด์„œ ์นด์šดํŠธํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

์ด ๋ถ€๋ถ„๋งŒ ์ž˜ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐ๋œ๋‹ค. 

 

import Foundation

struct Road {
    // x,y: ์›๋ž˜ ์žˆ๋˜ ๊ณณ nx,ny: ์ด๋™ํ•œ ๊ณณ 
    let x: Int
    let y: Int
    let nx: Int
    let ny: Int
}

func contains(_ roads: [Road], _ newRoad: Road) -> Bool {
    for road in roads {
        if road.x == newRoad.x && road.y == newRoad.y && road.nx == newRoad.nx && road.ny == newRoad.ny {
            return true
        }
        
        if road.x == newRoad.nx && road.y == newRoad.ny && road.nx == newRoad.x && road.ny == newRoad.y {
            return true
        }
    }
    
    return false
}

func solution(_ dirs:String) -> Int {
    var x = 5, y = 5
    var roads: [Road] = []
    for dic in dirs {
        let prevX = x, prevY = y
        switch dic {
            case "U": if x-1 >= 0 { x -= 1 }
            case "D": if x+1 <= 10 { x += 1 }
            case "L": if y-1 >= 0 { y -= 1 }
            case "R": if y+1 <= 10 { y += 1 }
            default:
            break
        }
        
        if x == prevX && y == prevY {
            continue
        }
        let road = Road(x: prevX, y: prevY, nx: x, ny: y)
        if !contains(roads, road) {
            roads.append(road)
        }
        
    }
    return roads.count
    
}

 

 

 ํ”ผ๋“œ๋ฐฑ

 

์ฒ˜์Œ์— ๋‚˜๋Š” ์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ๋“ค์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋ฒ”์œ„๊ฐ’๋งŒ ์ฒดํฌํ•˜๊ณ  ์‹ค์ œ ์—ฐ์‚ฐ๋ถ€๋ถ„์€ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ๋„ฃ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋ฒ”์œ„๊ฐ’์„ ์ฒดํฌํ•˜๋ฉด์„œ ์—ฐ์‚ฐ์„ํ•˜๊ณ  ๊ทธ ๋ถ€๋ถ„์ด ์œ ํšจํ•œ์ง€๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ฝ”๋“œ๊ฐ€ ์ค„๊ณ  ์ง๊ด€์ ์ด์˜€๋‹ค.

 

์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์ง€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด ํ•˜๋‚˜์˜ ๋ธ”๋Ÿญ์•ˆ์—์„œ ๋˜๋„๋ก ๋งŽ์ด ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค.