๐ŸŽ‡[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ต์ ์— ๋ณ„ ๋งŒ๋“ค๊ธฐ

Chobbyยท2022๋…„ 3์›” 5์ผ
0

Programmers

๋ชฉ๋ก ๋ณด๊ธฐ
9/349
post-thumbnail

ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์€ leego ๋‹˜์˜ ํ‹ฐ์Šคํ† ๋ฆฌ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Œ์„ ๋ฏธ๋ฆฌ ๋ฐํž™๋‹ˆ๋‹ค.

โ€ป 2๋‹จ๊ณ„ ๋ฌธ์ œ์ด๋ฉฐ ๊ฐ€์žฅ ์–ด๋ ค์›€

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ง€๋ฌธ์„ ์ฝ์œผ๋ฉด์„œ๋„ ๋ฉ˜ํƒˆ์ด ๋„ˆ๋ฌด ๋‚˜๊ฐ€์„œ ์ž์„ธํ•œ ์„ค๋ช…์„ ์ƒ๋žตํ•˜๊ณ  ๊ผญ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ฃผ์„์„ ๋‹ฌ๊ณ  ์ฒดํฌํ•˜๊ฒ ์Œ

์ถœ์ฒ˜ ) leego๋‹˜์˜ ํ‹ฐ์Šคํ† ๋ฆฌ ๋ธ”๋กœ๊ทธ

๋ฌธ์ œ ์‹œ์ž‘

Ax + By + C = 0์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” n๊ฐœ์˜ ์ง์„ ์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ด ์ง์„ ์˜ ๊ต์  ์ค‘ ์ •์ˆ˜ ์ขŒํ‘œ์— ๋ณ„์„ ๊ทธ๋ฆฌ๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ง์„  5๊ฐœ๋ฅผ

2x - y + 4 = 0
-2x - y + 4 = 0
-y + 1 = 0
5x - 8y - 12 = 0
5x + 8y + 12 = 0
์ขŒํ‘œ ํ‰๋ฉด ์œ„์— ๊ทธ๋ฆฌ๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด๋•Œ, ๋ชจ๋“  ๊ต์ ์˜ ์ขŒํ‘œ๋Š” (4, 1), (4, -4), (-4, -4), (-4, 1), (0, 4), (1.5, 1.0), (2.1, -0.19), (0, -1.5), (-2.1, -0.19), (-1.5, 1.0)์ž…๋‹ˆ๋‹ค. ์ด ์ค‘ ์ •์ˆ˜๋กœ๋งŒ ํ‘œํ˜„๋˜๋Š” ์ขŒํ‘œ๋Š” (4, 1), (4, -4), (-4, -4), (-4, 1), (0, 4)์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ •์ˆ˜๋กœ ํ‘œํ˜„๋˜๋Š” ๊ต์ ์— ๋ณ„์„ ๊ทธ๋ฆฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ์„ ๋ฌธ์ž์—ด๋กœ ๋‚˜ํƒ€๋‚ผ ๋•Œ, ๋ณ„์ด ๊ทธ๋ ค์ง„ ๋ถ€๋ถ„์€ *, ๋นˆ ๊ณต๊ฐ„(๊ฒฉ์ž์„ ์ด ๊ต์ฐจํ•˜๋Š” ์ง€์ )์€ .์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

"..........."  
".....*....."  
"..........."  
"..........."  
".*.......*."  
"..........."  
"..........."  
"..........."  
"..........."  
".*.......*."  
"..........."  

์ด๋•Œ ๊ฒฉ์žํŒ์€ ๋ฌดํ•œํžˆ ๋„“์œผ๋‹ˆ ๋ชจ๋“  ๋ณ„์„ ํฌํ•จํ•˜๋Š” ์ตœ์†Œํ•œ์˜ ํฌ๊ธฐ๋งŒ ๋‚˜ํƒ€๋‚ด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ •๋‹ต์€

"....*...."  
"........."  
"........."  
"*.......*"  
"........."  
"........."  
"........."  
"........."  
"*.......*"  

์ž…๋‹ˆ๋‹ค.

์ง์„  A, B, C์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด line์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ ๋ชจ๋“  ๋ณ„์„ ํฌํ•จํ•˜๋Š” ์ตœ์†Œ ์‚ฌ๊ฐํ˜•์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ฝ”๋”ฉ ์‹œ์ž‘

function solution(line) {
    // ์ •์ˆ˜์˜ ๊ต์ ์„ ๋ชจ๋‘ ์ฐพ๋Š”๋‹ค
    function findInterSectionPoints() {
        const meetPoints = []
        
        for(let i = 0 ; i < line.length ; i ++) {
            for(let j = i+1 ; j < line.length ; j ++) {
                // ํ˜„์žฌ ์ขŒํ‘œ ๋ฐ ๋‹ค์Œ ์ขŒํ‘œ์˜ x์ , y์ , ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•œ๋‹ค.
                const [curX,curY,curInc] = line[i]
                const [nextX,nextY,nextInc] = line[j] 
                
                // ๊ธฐ์šธ๊ธฐ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ํ‰ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ต์ ์ด ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค. 0 = ํ‰ํ–‰
                const Inclination = curX*nextY - curY*nextX
                
                if(Inclination) {
                    const meetX = (curY*nextInc - curInc*nextY) / Inclination
                    const meetY = (curInc*nextX - curX*nextInc) / Inclination
                    
                    if(Number.isInteger(meetX) && Number.isInteger(meetY)) {
                        meetPoints.push([meetX,meetY])
                    }
                }
            }
        }
        return meetPoints
    }
    
    // ์ฐพ์€ ๊ต์ ์„ ๋ณ„๋กœ ์ „ํ™˜ํ•˜๋ฉฐ ์ตœ์†Œ์˜ ๋ณด๋“œ ์˜์—ญ์„ ๊ทธ๋ฆผ
    function drawBoardAndFindStar(meetPoints) {
        // ๊ต์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ตœ์†Œ์˜์—ญ์˜ ๊ต์ ์„ ๋„ฃ๋Š”๋‹ค.
        const [maxX, maxY, minX, minY] = meetPoints.reduce(
            ([maxX, maxY, minX, minY], [x, y]) => [
                Math.max(maxX,x),
                Math.max(maxY,y),
                Math.min(minX,x),
                Math.min(minY,y)
            ],
            [
                Number.MIN_SAFE_INTEGER,
                Number.MIN_SAFE_INTEGER,
                Number.MAX_SAFE_INTEGER,
                Number.MAX_SAFE_INTEGER,
            ]
        )
        
        // ์ตœ์†Œํ™” ํ•  ์ˆ˜ ์žˆ๋Š” X, Y ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ณด๋“œ๋ฅผ ๊ทธ๋ฆฐ๋‹ค.
        let board = Array.from(Array(maxY-minY+1), () => Array(maxX-minX+1).fill('.'))

        // ๊ทธ๋ฆฐ ๋ณด๋“œ์— * ์„ ์‚ฝ์ž…ํ•œ๋‹ค
        meetPoints.forEach(([x,y]) => {
            board[maxY-y][x-minX] = '*'
        })
        
        return board.map(x => x.join(""))
    }
    
    return drawBoardAndFindStar(findInterSectionPoints())
}

์ดํ•ดํ•˜๊ธฐ ์กฐ์ฐจ ๋งŽ์ด ๋ฒ„๊ฑฐ์› ๋˜ ๋ฌธ์ œ์ธ๋งŒํผ ์˜ค๋ž˜ ๊ธฐ์–ต์— ๋‚จ์„ ๊ฒƒ ๊ฐ™๋‹ค.

profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ

0๊ฐœ์˜ ๋Œ“๊ธ€