Skip to content

Commit 9fd2629

Browse files
committed
[bruteforce] DiceRoll
1 parent 87308ec commit 9fd2629

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

build.gradle.kts

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
import org.jetbrains.kotlin.konan.properties.Properties
3-
41
plugins {
52
kotlin("jvm") version "1.8.20"
63
id("java-library")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package baekjun.bruteforce
2+
3+
4+
// https://www.acmicpc.net/problem/14499
5+
6+
private val graph = Array<IntArray>(20) { IntArray(20) }
7+
private val dicex = IntArray(4) // dicex[1] 위, dicex[3] 아래
8+
private val dicey = IntArray(4) // dicey[1] 위, dicey[3] 아래
9+
private var commanders = IntArray(1000)
10+
private val dx = listOf(0, 0, -1, 1) // <-, -> , 위, 아래
11+
private val dy = listOf(1, -1, 0, 0)
12+
private var x = 0
13+
private var y = 0
14+
private var n = 0
15+
private var m = 0
16+
private var ans = mutableListOf<Int>()
17+
fun main() {
18+
val (_n, _m, _x, _y) = readln().split(" ").map { it.toInt() }
19+
n = _n
20+
m = _m
21+
x = _x
22+
y = _y
23+
repeat(n) { i ->
24+
val t = readln().split(" ").map { it.toInt() }.toIntArray()
25+
graph[i] = t
26+
}
27+
commanders = readln().split(" ").map { it.toInt() }.toIntArray()
28+
29+
commanders.forEach { rollDice(it) }
30+
31+
ans.forEach { println(it) }
32+
}
33+
34+
private fun rollDice(c: Int) {
35+
val cmd = c - 1
36+
// 범위 체크
37+
val _x = x + dx[cmd]
38+
val _y = y + dy[cmd]
39+
if (((_x in 0 until n) and (_y in 0 until m)).not()) return
40+
// 주사위 이동
41+
x = _x
42+
y = _y
43+
// 주사위 복사
44+
val _dicex = dicex.copyOf()
45+
val _dicey = dicey.copyOf()
46+
47+
// 주사위 굴리기
48+
when (cmd) {
49+
0 -> { // <=
50+
dicex.shift(-1)
51+
dicey[1] = dicex[1]
52+
dicey[3] = dicex[3]
53+
}
54+
55+
1 -> { // =>
56+
dicex.shift(1)
57+
dicey[1] = dicex[1]
58+
dicey[3] = dicex[3]
59+
}
60+
61+
2 -> { //
62+
dicey.shift(-1)
63+
dicex[1] = dicey[1]
64+
dicex[3] = dicey[3]
65+
}
66+
67+
3 -> { // 아래
68+
dicey.shift(+1)
69+
dicex[1] = dicey[1]
70+
dicex[3] = dicey[3]
71+
}
72+
}
73+
addNumAndChangeNum()
74+
// println("dicex : ${dicex.toList()}")
75+
// println("dicey : ${dicey.toList()}")
76+
// println()
77+
}
78+
79+
private fun IntArray.shift(amount: Int) {
80+
val _arr = this.copyOf()
81+
val provideIdx: (Int) -> Int = { (it + size + amount) % size }
82+
repeat(size) { i ->
83+
val idx = provideIdx(i)
84+
this[idx] = _arr[i]
85+
}
86+
return
87+
}
88+
89+
private fun addNumAndChangeNum() {
90+
ans.add(dicex[1]) // 현재 상단면 ans에 추가
91+
// 바닥면 바꾸기
92+
if (graph[x][y] == 0) {// 바닥에 복사
93+
graph[x][y] = dicex[3]
94+
} else { // 주사위에 바닥 복사 후 바닥 0
95+
dicex[3] = graph[x][y]
96+
dicey[3] = graph[x][y]
97+
graph[x][y] = 0
98+
}
99+
}

0 commit comments

Comments
 (0)