๐ ๋ฌธ์
โ ๏ธ ์ค์ ๋ ธํธ
1๋ฒ ~ 6๋ฒ์ ๋ํด ์ ๊ฐ๋๊ฐ ์์ผ๋ฏ๋ก
๋์๋จ๋ถ ์ด๋์ ์ค๊ฒ๋๋ ์ ์นธ์ ์ ์ฅํ๋ 6๊ฐ ๋ฉด์ ์๋ฏธํ๋ ๊ฐ์ฒด ์์ฑ
๊ฐ ๊ฐ์ฒด๋ ๊ฐ์ ๊ฐ์ง๊ณ ๊ฐ์ฒด๋ณ๋ก ๋์๋จ๋ถ์ผ๋ก ์ฃผ์ฌ์ ๋ฐฉํฅ์ด ์ด๋ํ์ ๋ ํํํ๋ ๊ฐ์ฒด ๊ฐ์ ๊ณ ์ ํด๋ .
์ด 6๊ฐ ๋ฉด์ ๋ํด ๋์๋จ๋ถ ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ์ ํํ๋๋ ์์นธ์ '๊ณ ์ 'ํ๋ฉด ์๋๋ค.
๊ตด๋ฆฌ๋ฉด์ ๋ณด๋ ๊ฐ๋๊ฐ ๋ฌ๋ผ์ง ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
// ์ ๊ฐ๋์ ๊ฐ ๋ฉด์๋ํด data class ๋ฅผ ๋ฏธ๋ฆฌ ๋ง๋ค๊ณ ์ด๊ธฐํ
// โ ๏ธ ํ๋ฆฐ ์ ๊ทผ ๋ฐฉ๋ฒ
data class DiceSquare(var curVal: Int = 0, var east: DiceSquare?, var west: DiceSquare?, var south: DiceSquare?, var north: DiceSquare?, var opposite: DiceSquare?, val id: Int) {
}
private fun initializeDice(dice1: DiceSquare, dice2: DiceSquare, dice3: DiceSquare, dice4: DiceSquare, dice5: DiceSquare, dice6: DiceSquare) {
dice1.east = dice4
dice1.west = dice3
dice1.south = dice2
dice1.north = dice5
dice1.opposite = dice6
dice2.east = dice4
dice2.west = dice3
dice2.south = dice6
dice2.north = dice1
dice2.opposite = dice5
dice3.east = dice2
dice3.west = dice5
dice3.south = dice6
dice3.north = dice1
dice3.opposite = dice4
dice4.east = dice6
dice4.west = dice1
dice4.south = dice2
dice4.north = dice5
dice4.opposite = dice3
dice5.east = dice4
dice5.west = dice3
dice5.south = dice1
dice5.north = dice6
dice5.opposite = dice2
dice6.east = dice4
dice6.west = dice3
dice6.south = dice5
dice6.north = dice2
dice6.opposite = dice1
}
๐ง ์์ด๋์ด
์ฐ์ ์ ๊ฐ๋๋ฅผ ์กฐ๋ฆฝํด์ ์ฃผ์ฌ์ ๋ชจ์์ ๊ทธ๋ ค๋ณด์
์ ๊ฐ๋ ์กฐ๋ฆฝํ ์นธ์ ๋งคํ๋๋ ๋ฐฉํฅ์ ๋ฏธ๋ฆฌ enum class ๋ก ๋ค์๊ณผ๊ฐ์ด ์ก์์ค๋ค.
1๋ฒ์นธ | UP |
2๋ฒ์นธ | FRONT |
3๋ฒ์นธ | RIGHT |
4๋ฒ์นธ | LEFT |
5๋ฒ์นธ | BACK |
6๋ฒ์นธ | BOTTOM |
์ด์ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ ค ๋,์,๋จ,๋ถ์ ๋ํด ๋ณ๊ฒฝ๋๋ ๋ฉด์ ์๋ก ๋งคํ์์ผ์ฃผ๊ธฐ๋ง ํ๋ฉด๋๋ค.
๋์ชฝ์ผ๋ก ๊ตด๋ฆฌ๋ฉด ์์๊ฐ์ด ๊ฐ ๋ฉด์ด ๋์น๋๋ค.
๋จ, ์ฃผ์ฌ์๋ฅผ ๊ตด๋ ค ์นธ ์ด๋์ ๊ฒ์ ํ์ ๋ฒ์ด๋์ง ์๋์ง ๋งค๋ฒ ๋ฒ์๋ฅผ ๊ฒ์ฌํด์ผํ๋ค.
๐ Kotlin Code
package roolthedice
import java.io.BufferedReader
import java.io.InputStreamReader
//๋์ชฝ์ 1, ์์ชฝ์ 2, ๋ถ์ชฝ์ 3, ๋จ์ชฝ์ 4
private val dx = intArrayOf(0, 0, 0, -1, 1)
private val dy = intArrayOf(0, 1, -1, 0, 0)
enum class DiceDirection() {
UP,
FRONT,
RIGHT,
LEFT,
BACK,
BOTTOM
}
private fun rotateDice(diceArr: IntArray, command: Int) {
//๋์ชฝ์ 1, ์์ชฝ์ 2, ๋ถ์ชฝ์ 3, ๋จ์ชฝ์ 4
when (command) {
1 -> {
val tmp = diceArr[DiceDirection.UP.ordinal]
diceArr[DiceDirection.UP.ordinal] = diceArr[DiceDirection.LEFT.ordinal]
diceArr[DiceDirection.LEFT.ordinal] = diceArr[DiceDirection.BOTTOM.ordinal]
diceArr[DiceDirection.BOTTOM.ordinal] = diceArr[DiceDirection.RIGHT.ordinal]
diceArr[DiceDirection.RIGHT.ordinal] = tmp
}
2 -> {
val tmp = diceArr[DiceDirection.UP.ordinal]
diceArr[DiceDirection.UP.ordinal] = diceArr[DiceDirection.RIGHT.ordinal]
diceArr[DiceDirection.RIGHT.ordinal] = diceArr[DiceDirection.BOTTOM.ordinal]
diceArr[DiceDirection.BOTTOM.ordinal] = diceArr[DiceDirection.LEFT.ordinal]
diceArr[DiceDirection.LEFT.ordinal] = tmp
}
3 -> {
val tmp = diceArr[DiceDirection.UP.ordinal]
diceArr[DiceDirection.UP.ordinal] = diceArr[DiceDirection.FRONT.ordinal]
diceArr[DiceDirection.FRONT.ordinal] = diceArr[DiceDirection.BOTTOM.ordinal]
diceArr[DiceDirection.BOTTOM.ordinal] = diceArr[DiceDirection.BACK.ordinal]
diceArr[DiceDirection.BACK.ordinal] = tmp
}
4 -> {
val tmp = diceArr[DiceDirection.UP.ordinal]
diceArr[DiceDirection.UP.ordinal] = diceArr[DiceDirection.BACK.ordinal]
diceArr[DiceDirection.BACK.ordinal] = diceArr[DiceDirection.BOTTOM.ordinal]
diceArr[DiceDirection.BOTTOM.ordinal] = diceArr[DiceDirection.FRONT.ordinal]
diceArr[DiceDirection.FRONT.ordinal] = tmp
}
}
}
fun main() {
val bufferedReader = BufferedReader(InputStreamReader(System.`in`))
val(row, col, startRow, startCol) = bufferedReader.readLine().split(" ").map(String::toInt)
val stringBuilder = StringBuilder()
val map = Array(row){IntArray(col)}
for (idx in map.indices) {
map[idx] = bufferedReader.readLine().split(" ").map(String::toInt).toIntArray()
}
val commands = bufferedReader.readLine().split(" ").map(String::toInt).toIntArray()
var nextRow = startRow
var nextCol = startCol
val diceArr = intArrayOf(0, 0, 0, 0, 0, 0)
for (command in commands) {
nextRow += dx[command]
nextCol += dy[command]
if (nextRow < 0 || nextCol < 0 || nextRow >= row || nextCol >= col) {
// ๋ฒ์๋ฅผ ๋์ด์ ๊ฒฝ์ฐ ์์ ๋ณต๊ตฌ
nextRow -= dx[command]
nextCol -= dy[command]
continue
}
// ์ฃผ์ฌ์ ํ์ ์ฒ๋ฆฌ
rotateDice(diceArr, command)
stringBuilder.append(diceArr[DiceDirection.UP.ordinal]).appendLine()
if (map[nextRow][nextCol] == 0) map[nextRow][nextCol] = diceArr[DiceDirection.BOTTOM.ordinal]
else {
diceArr[DiceDirection.BOTTOM.ordinal] = map[nextRow][nextCol]
map[nextRow][nextCol] = 0
}
}
print(stringBuilder.toString())
}
'Algorithm > Algorithm (๋ฌธ์ ํ์ด)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 14891 ํฑ๋๋ฐํด (0) | 2022.10.25 |
---|---|
[๋ฐฑ์ค] 2457 ๊ณต์ฃผ๋์ ์ ์ (0) | 2022.10.21 |
[๋ฐฑ์ค] 11559 Puyo Puyo (2) | 2022.10.13 |
[๋ฐฑ์ค] 10799 ์ ๋ง๋๊ธฐ (0) | 2022.10.11 |
[๋ฐฑ์ค] 15686 ์นํจ ๋ฐฐ๋ฌ (1) | 2022.10.10 |