๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ™‚ | Silver

[Kotlin, S3] ๋ฐฑ์ค€ 8911๋ฒˆ ๊ฑฐ๋ถ์ด

by immgga 2024. 8. 30.

์ถœ์ฒ˜: unsplash.com

 

๊ฑฐ๋ถ์ด(8911๋ฒˆ)

Silver 3

#๊ตฌํ˜„ #์‹œ๋ฎฌ๋ ˆ์ด์…˜

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

์ฒ˜์Œ์— ๊ฑฐ๋ถ์ด๊ฐ€ ์ขŒํ‘œ 0, 0์— ์žˆ์„ ๋•Œ, ์ด ๊ฑฐ๋ถ์ด๋ฅผ ๋ช…๋ น์–ด๋กœ ์›€์ง์—ฌ์„œ ๊ฑฐ๋ถ์ด๊ฐ€ ์ง€๋‚˜๊ฐ„ ์˜์—ญ์˜ ์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•œ๋‹ค.

์ด ๊ฒฝ์šฐ์—๋Š” ๊ฑฐ๋ถ์ด๊ฐ€ ์›€์ง์ด๊ณ  ๋‚œ ์ขŒํ‘œ x, y๋ฅผ ๋ชจ๋‘ ๊ตฌํ•ด์„œ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ์ฐจ๋ฅผ ์ด์šฉํ•ด ๊ฐ ๋ณ€์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๊ณ , x์˜ ๊ฑฐ๋ฆฌ์™€

y์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณฑํ•ด์ฃผ๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์šฐ์„  ๋ช…๋ น์— ๋”ฐ๋ฅธ ๊ฐ๋„ ๊ณ„์‚ฐ ๋ฐ ์ด๋™ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

 

<์กฐ๊ฑด 1>

๊ฐ๋„์˜ ๊ฒฝ์šฐ๋Š” ์ž์‹ ์ด ํŽธํ•˜๊ฒŒ ์ •์˜ํ•˜๋ฉด ๋œ๋‹ค. ๋‚ด ๊ฒฝ์šฐ์—๋Š” 0, 90, 180, 270์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

'L' -> {
    if (currentAngle == 0) currentAngle = 270
    else currentAngle -= 90
}
'R' -> {
    if (currentAngle == 270) currentAngle = 0
    else currentAngle += 90
}

์™ผ์ชฝ์œผ๋กœ ๋Œ ๋•Œ๋Š” ๊ฐ๋„๋ฅผ ๋นผ์ฃผ๊ณ , ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋Œ ๋•Œ๋Š” ๊ฐ๋„๋ฅผ ๋”ํ•ด ์ค€๋‹ค. ๊ฐ๋„์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” ๊ธฐ๋ณธ์ด๋‹ค.

 

 

<์กฐ๊ฑด 2>

์ด์ œ ์•ž, ๋’ค๋กœ ์›€์ง์ด๋ผ๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

ํ˜„์žฌ ์ขŒํ‘œ์—์„œ ์›€์ง์ธ ๊ฒฐ๊ณผ๋ฅผ return ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ๋‹ค.

private fun getMoveRes(x: Int, y: Int, currentAngle: Int, isBack: Int): List<Int> {
    return when (currentAngle) {
        0 -> listOf(x, y + (1 * isBack))
        90 -> listOf(x + (1 * isBack), y)
        180 -> listOf(x, y - (1 * isBack))
        270 -> listOf(x - (1 * isBack), y)
        else -> listOf()
    }
}

ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์— front, back move๋ฅผ ์ฒ˜๋ฆฌํ•  ๊ฑฐ๊ธฐ ๋•Œ๋ฌธ์— front์ธ์ง€ back์ธ์ง€ ์ฒ˜๋ฆฌํ•  ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค.

๊ฐ ๊ฐ๋„์— ๋”ฐ๋ผ ํ˜„์žฌ ์ขŒํ‘œ์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด ์ค€๋‹ค.

isBack ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ๋Š” front์ผ ๋•Œ๋Š” 1์„, back์ผ ๋•Œ๋Š” -1์„ ๋„ฃ์–ด์„œ ๋’ค๋กœ ๊ฐˆ ๋•Œ๋Š” -1์„ ๊ณฑํ•œ ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val case = readLine().toInt()

    for (i in 0 until case) {
        val command = readLine()
        val moveXLocations = mutableListOf(0)
        val moveYLocations = mutableListOf(0)
        var currentAngle = 0

        for (cmd in command) {
            when (cmd) {
                'F' -> {
                    val moveRes = getMoveRes(moveXLocations.last(), moveYLocations.last(), currentAngle, 1)
                    moveXLocations.add(moveRes.first())
                    moveYLocations.add(moveRes.last())
                }
                'B' -> {
                    val moveRes = getMoveRes(moveXLocations.last(), moveYLocations.last(), currentAngle, -1)
                    moveXLocations.add(moveRes.first())
                    moveYLocations.add(moveRes.last())
                }
                'L' -> {
                    if (currentAngle == 0) currentAngle = 270
                    else currentAngle -= 90
                }
                'R' -> {
                    if (currentAngle == 270) currentAngle = 0
                    else currentAngle += 90
                }
            }
        }

        val xDistance = with(moveXLocations) { max() - min() }
        val yDistance = with(moveYLocations) { max() - min() }

        println(xDistance * yDistance)
    }
}

// back: -1, front: 1
private fun getMoveRes(x: Int, y: Int, currentAngle: Int, isBack: Int): List<Int> {
    return when (currentAngle) {
        0 -> listOf(x, y + (1 * isBack))
        90 -> listOf(x + (1 * isBack), y)
        180 -> listOf(x, y - (1 * isBack))
        270 -> listOf(x - (1 * isBack), y)
        else -> listOf()
    }
}

 

๋ฌธ์ œ ํ’€์ด

๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋ช…๋ น์–ด์— ๋งž๊ฒŒ ์กฐ๊ฑด 1, ์กฐ๊ฑด 2๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์ขŒํ‘œ๊ฐ€ ๋ณ€ํ•˜๊ฒŒ ๋˜๋ฉด(F, B๋ฅผ ๋ช…๋ น๋ฐ›์€ ๊ฒฝ์šฐ) ํ˜„์žฌ x, y ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•˜๋Š” moveXLocations, moveYLocations์— ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

 

๋ช…๋ น์ด ๋๋‚˜๊ณ  ๋‚˜๋ฉด ๋“ค์–ด ์žˆ๋Š” moveX, moveY ๋ฆฌ์ŠคํŠธ ์ค‘ ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„ ๋‘ ๊ฐ’์˜ ์ฐจ๋ฅผ ๊ตฌํ•˜๋ฉด ๊ทธ ๊ฐ’์ด ํ•ด๋‹น ์ขŒํ‘œ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋œ๋‹ค. ์ฆ‰ ์‚ฌ๊ฐํ˜•์˜ ๋ณ€์ด ๋œ๋‹ค.

x์˜ ๊ฑฐ๋ฆฌ, y์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด ๊ณฑํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

๊ฐ๋„๋ฅผ ์ด์šฉํ•ด ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ขŒํ‘œ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ.

ํŠน์ • ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋“ค์–ด๊ฐ€์ง„ ์•Š๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ.

728x90