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

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ ๊ต์ ์— ๋ณ„ ๋งŒ๋“ค๊ธฐ

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



Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ต์ ์— ๋ณ„ ๋งŒ๋“ค๊ธฐ

[[2, -1, 4], [-2, -1, 4], [0, -1, 1], [5, -8, -12], [5, 8, 12]] ["....*....", ".........", ".........", "*.......*", ".........", ".........", ".........", ".........", "*.......*"] [[0, 1, -1], [1, 0, -1], [1, 0, 1]] ["*.*"] [[1, -1, 0], [2, -1, 0], [4, -

programmers.co.kr


Solution

 

ํ•ด๋‹น ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๋‘ ์ง์„ (๋ฐฉ์ •์‹)์˜ ๊ต์ ์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์„ ์•„๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์•„๋ž˜ ๋‘ ์ง์„ ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1. ax + by + c
2. dx + ey + f

 

๊ต์ ์„ ์ฐพ๋Š” ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

x = (bf - ed) / (ad - bc)
y = (ec - af) / (ad - bc)

 

์—ฌ๊ธฐ์„œ ad - bc๊ฐ€ ๋งŒ์•ฝ 0์ด๋ผ๋ฉด ๋‘ ์ง์„ ์€ ํ‰ํ–‰ํ•˜๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

 

์œ„ ๋ฐฉ์‹๋Œ€๋กœ ๋ชจ๋“  ์ง์„ ์˜ ๊ต์ ์„ ์ฐพ์œผ๋ฉฐ x์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’๊ณผ ์ž‘์€ ๊ฐ’, y์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’๊ณผ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ ์ตœ์†Œ ์‚ฌ๊ฐํ˜•์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฒ”์œ„ ์•ˆ์—์„œ ๊ต์ ์˜ ์œ„์น˜๋ฅผ ๋ณ„๋กœ ๋ฐ”๊ฟ”์ค€ ๋’ค ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ฝ”๋“œ ํ•ด์„ค

 

๊ต์ ,์ตœ์†Œ ์‚ฌ๊ฐํ˜•,์ง์„ ์˜ ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

struct Line {
    let x:Int,y:Int,c:Int
}

struct Square {
    var minX:Int = Int.max
    var maxX:Int = -Int.max
    var minY:Int = Int.max
    var maxY:Int = -Int.max
}

struct Meet {
    let x:Int,y:Int
}

 

1. ๊ต์ ์„ ์ฐพ๋Š”๋‹ค.

 

์œ„์—์„œ ๊ต์ ์„ ๊ตฌํ•˜๋Š” ์‹์œผ๋กœ ๋‘ ์ง์„ ์˜ ๊ต์ ์„ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค.

 

๋‹ค๋งŒ ๋ฌธ์ œ์—์„œ x์™€ y ๋ชจ๋‘ ์ •์ˆ˜์ธ ๊ต์ ๋งŒ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ •์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒƒ์€ nil๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

func findMeet(_ l1:Line,_ l2:Line) -> Meet? {
    let adbc = l1.x*l2.y - l1.y*l2.x
    if adbc == 0 { return nil }
    let bfed = l1.y*l2.c - l1.c*l2.y
    let ecaf = l1.c*l2.x - l1.x*l2.c
    let meet = Meet(x:bfed/adbc, y:ecaf/adbc)
    return bfed % adbc == 0 && ecaf % adbc == 0 ? meet : nil
}

 

2. ๋ชจ๋“  ์ง์„ ์˜ ๊ต์ ์„ ๊ตฌํ•œ๋‹ค.

 

๋ชจ๋“  ์ง์„ ์˜ ๊ต์ ์„ ์ฐพ์•„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

func getIntegerMeet(lines:[[Int]]) -> [Meet] {
    var lines:[[Int]] = lines
    var meets:[Meet] = []
    while !lines.isEmpty {
        let first = lines.removeFirst()
        let firstLine = Line(x:first[0], y:first[1], c: first[2])
        for l in lines {
            let otherLine = Line(x: l[0], y: l[1], c: l[2])
            if let meet = findMeet(firstLine,otherLine) {
                meets.append(meet)
            }
        }
    }
    return meets
}

 

3. ๋ชจ๋“  ๊ต์ ๋“ค์„ ์ด์šฉํ•ด ์ตœ์†Œ ์‚ฌ๊ฐํ˜•์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•œ๋‹ค.

 

func getSquare(meets:[Meet]) -> Square {
    var square:Square = Square()
    meets.forEach {
        square.minX = min(square.minX,$0.x)
        square.maxX = max(square.maxX,$0.x)
        square.minY = min(square.minY,$0.y)
        square.maxY = max(square.maxY,$0.y)
    }
    return square
}

 

4. ์ตœ์†Œ ์‚ฌ๊ฐํ˜•์— ๊ต์ ์„ ๋ณ„๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

 

x์˜ ์ตœ๋Œ€๊ฐ’ - ์ตœ์†Œ๊ฐ’ + 1๋งŒํผ์˜ ๋ฐฐ์—ด์„ y์˜ ์ตœ๋Œ“๊ฐ’ - ์ตœ์†Œ๊ฐ’ + 1๋งŒํผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

๊ทธ ๋‹ค์Œ ๊ต์ ๋“ค์„ ํ™•์ธํ•˜๋ฉด์„œ ๋ณ„๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

 

์ฃผ์˜ํ•˜์‹ค ์ ์€ ์ขŒํ‘œ์—์„œ y๊ฐ’์€ ์œ„๋กœ ๊ฐˆ์ˆ˜๋ก ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋Œ“๊ฐ’์—์„œ ํ˜„์žฌ y๊ฐ’์„ ๋นผ์ค๋‹ˆ๋‹ค.

 

func changeMeetsToStar(meets:[Meet],square:Square) -> [String] {
    var sqaureDot = Array(repeating: Array(repeating: ".", count: square.maxX-square.minX+1), count: square.maxY-square.minY+1)
    for meet in meets {
        let x = meet.x - square.minX
        let y = square.maxY - meet.y
        sqaureDot[y][x] = "*"
    }
    return sqaureDot.map{$0.joined()}
}

 

5. ๋ณ„๋กœ ๋ฐ”๊พผ ์‚ฌ๊ฐํ˜•์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

func solution(_ line:[[Int]]) -> [String] {
    let meets:[Meet] = getIntegerMeet(lines:line)
    let square = getSquare(meets: meets)
    return changeMeetsToStar(meets: meets, square: square)
}

Source Code

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€