๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ˜€ | Bronze

[Kotlin, B1] ๋ฐฑ์ค€ 1952๋ฒˆ ๋‹ฌํŒฝ์ด2

by immgga 2024. 8. 14.

์ถœ์ฒ˜: unsplash.com

 

๋‹ฌํŒฝ์ด 2(1952๋ฒˆ)

Bronze 1

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

https://www.acmicpc.net/problem/1952

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

M * N์˜ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ๋Œ๋ฉด์„œ ๊บพ์ธ ์ง€์ ์ด ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์ฃผ์˜ํ•ด์•ผ ํ•˜๋Š” ์ ์€ ํ•œ ๋ฒˆ ์ง€๋‚œ ๋ถ€๋ถ„์€ ๋‹ค์‹œ ์ง€๋‚˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ˆ˜ํ•™์ ์œผ๋กœ ๊ทœ์น™์„ ์ฐพ์•„์„œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ๋ฉ์ฒญํ•œ ๊ด€๊ณ„๋กœ ๋ฌด์ง€์„ฑ ์ „์ฒด ํƒ์ƒ‰์„ ํ•  ๊ฒƒ์ด๋‹ค.

 

2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ booleanํ˜•์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉด ํ•œ ๋ฒˆ ์ง€๋‚œ ๋ถ€๋ถ„์„ true๋กœ ์„ค์ •ํ•˜๊ณ  ์กฐ๊ฑด์„ ๋‹ฌ๋ฉด ๋ผ์„œ ํ•œ ๋ฒˆ ์ง€๋‚œ ๋ถ€๋ถ„์„ ์ฒดํฌํ•  ๋•Œ ํŽธํ•˜๋‹ค.

 

 

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

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

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (h, w) = readLine().split(" ").map { it.toInt() }
    val map = Array(h) { BooleanArray(w) }
    val moveX = listOf(1, 0, -1, 0)
    val moveY = listOf(0, 1, 0, -1)
    var res = 0

    map[0][0] = true

    var currentX = 0
    var currentY = 0
    var m = 0

    while (true) {
        while (currentY in map.indices && currentX in 0 until map[currentY].size) {
            val mx = moveX[m] + currentX
            val my = moveY[m] + currentY

            if (my in map.indices && mx in 0 until map[my].size) {
                if (!map[my][mx]) {
                    map[my][mx] = true

                    currentX = mx
                    currentY = my
                } else break

            } else break
        }

        val afterMove = if (m == 3) 0 else m + 1
        val afterX = currentX + moveX[afterMove]
        val afterY = currentY + moveY[afterMove]

        if (afterY in map.indices && afterX in 0 until map[0].size && !map[afterY][afterX]) {
            res++
            m = afterMove
        } else break
    }

    println(res)
}

 

๋ฌธ์ œ ํ’€์ด

์ž…๋ ฅ๋ฐ›์€ m, n์„ ๊ธฐ์ค€์œผ๋กœ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ด๋™ํ•  ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ผ moveX, moveY๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. moveX, moveY๋ฅผ ์ˆœํ™˜์‹œํ‚ค๋ฉด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•  ๊ฒƒ์ด๋‹ค.

 

์‹œ์ž‘ ๋ถ€๋ถ„์ธ 0, 0์„ true๋กœ ์ง€์ •ํ•ด ์ฃผ๊ณ , ์ง€์ •ํ•œ move๋งŒํผ ์ด๋™ํ•˜๋ฉด์„œ list์˜ ๋๊นŒ์ง€ ์ด๋™ํ•œ๋‹ค.

์ด๋™ํ•˜๋ฉด์„œ map์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋„ true๋กœ ์„ค์ •ํ•ด ์ค€๋‹ค.

์ด๋™ํ•˜๋ฉด์„œ ์›€์ง์ธ index๋Š” ๊ณ„์† ์ €์žฅํ•ด ์ค€๋‹ค.

 

๋๊นŒ์ง€ ์ด๋™ํ•˜๊ณ  ๋‚˜๋ฉด ์ด์ œ ๋ฐฉํ–ฅ์„ ๊บพ์–ด์•ผ ํ•œ๋‹ค.

๊บพ์„ ๋ฐฉํ–ฅ์„ afterMove๋กœ move์˜ index๋ฅผ ์ง€์ •ํ•ด ๋‹ค์Œ์— ์›€์ง์ผ ๋ฐฉํ–ฅ์ด ๋ฆฌ์ŠคํŠธ ์•ˆ์— ํฌํ•จ๋˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฉ๋ฌธ์ด ์•ˆ๋œ ๊ฐ’์ผ ๋•Œ ๊บพ์€ ํšŸ์ˆ˜๋ฅผ ๋Š˜๋ ค ์ฃผ๊ณ  m์„ afterMove๋กœ ๊ต์ฒดํ•œ๋‹ค.

 

์ด๋Ÿฌ๋ฉด ๊ณ„์† moveX, moveY์—์„œ ์„ค์ •ํ•œ ์ด๋™ ๊ฒฝ๋กœ์— ๋”ฐ๋ผ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ๋Œ๊ฒŒ ๋œ๋‹ค.

 

 

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

์ˆ˜ํ•™์ ์œผ๋กœ ํ’€๋ฉด ์‰ฝ๋‹ค. ํ•˜์ง€๋งŒ ์•ˆ ํ•ด๋ด์„œ ๋ชจ๋ฅธ๋‹ค. ํ•˜์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ์งง์•„์ง€๊ณ  ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•  ๊ฒƒ์ด๋‹ค.

๋‚˜๋Š” ์ˆ˜ํ•™์ ์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ ค๋‹ค๊ฐ€ ๊ทœ์น™์„ ๋ชป ์ฐพ๊ณ  ์ „์ฒด ํƒ์ƒ‰์œผ๋กœ ๋ฐ”๊ฟจ๋‹ค.

์ „์ฒด ํƒ์ƒ‰์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€์™€ index๊ฐ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฒ—์–ด๋‚˜์ง€๋Š” ์•Š๋Š”์ง€ ์ฒดํฌํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ตฌํ˜„๋„ ๊ท€์ฐฎ๊ณ  ์‹ค์ˆ˜๊ฐ€ ๋งŽ์ด ๋‚  ์ˆ˜ ์žˆ๋‹ค.

728x90