๐ ๋ฌธ์
๐ง ์์ด๋์ด
ํฑ๋๋ฐํด ํ์ ์์ด๋์ด
์๊ณ ๋ฐฉํฅ
๋ฐ์๊ณ ๋ฐฉํฅ
์๋ฃ๊ตฌ์กฐ๋ก ๋ฑ ์ฌ์ฉ
ํ์ ๋ฐฉํฅ์ ๊ฒฐ์ ํ๋ ๊ฒ์ index2, 6
๐ Kotlin Code
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import kotlin.collections.ArrayDeque
private fun rotateGears(gearNum: Int, gears: Array<ArrayDeque<Int>>, gearRotateDirection: IntArray) {
when (gearRotateDirection[gearNum]) {
-1 -> {
gears[gearNum].addLast(gears[gearNum].removeFirst())
}
1 -> {
gears[gearNum].addFirst(gears[gearNum].removeLast())
}
else -> {}
}
}
private fun decideRotate(gears: Array<ArrayDeque<Int>>, leftGearNum: Int, rightGearNum: Int, isDecided: BooleanArray, gearRotateDirection: IntArray) {
if (leftGearNum <= 0 || rightGearNum >= gears.size) return
if (isDecided[leftGearNum] && isDecided[rightGearNum]) return
// ๊ฐ์ ๊ทน์ด๋ฉด ๋ฐฉํฅ ๊ฒฐ์ ํ ์๋ฌด๊ฒ๋ ํ ๊ฒ ์์.
if (gears[leftGearNum][2] == gears[rightGearNum][6]) {
isDecided[leftGearNum] = true
isDecided[rightGearNum] = true
return
} else {
// ์งํ๋ฐฉํฅ ์ผ์ชฝ
if (!isDecided[leftGearNum]) {
gearRotateDirection[leftGearNum] = -gearRotateDirection[rightGearNum]
isDecided[leftGearNum] = true
decideRotate(gears, leftGearNum - 1, leftGearNum, isDecided, gearRotateDirection)
}
// ์งํ๋ฐฉํฅ ์ค๋ฅธ์ชฝ
if (!isDecided[rightGearNum]) {
gearRotateDirection[rightGearNum] = -gearRotateDirection[leftGearNum]
isDecided[rightGearNum] = true
decideRotate(gears, rightGearNum, rightGearNum + 1, isDecided, gearRotateDirection)
}
}
}
fun main() {
val bufferedReader = BufferedReader(InputStreamReader(System.`in`))
// index: gear number, value: element (0, 1)
val gears = Array(5){ArrayDeque<Int>(8)}
// index: gear number, value: rotate direction (-1 : counter clock wise, 0: no rotation, 1: clockwise)
val gearRotateDirection = IntArray(5)
// index: gear number, value: is rotation direction is decided for each round
val isDecided = BooleanArray(5)
for (i in 1 .. 4) {
bufferedReader.readLine().toCharArray().map { it - '0' }.forEach { element-> gears[i].addLast(element) }
}
val rotateCnt = bufferedReader.readLine().toInt()
repeat(rotateCnt) {
val (gearNum, rotateDirection) = bufferedReader.readLine().split(" ").map(String::toInt)
gearRotateDirection[gearNum] = rotateDirection
isDecided[gearNum] = true
decideRotate(gears, gearNum, gearNum + 1, isDecided, gearRotateDirection)
decideRotate(gears, gearNum - 1, gearNum, isDecided, gearRotateDirection)
for (gearNumber in 1 .. 4) rotateGears(gearNumber, gears, gearRotateDirection)
Arrays.fill(gearRotateDirection, 0)
Arrays.fill(isDecided, false)
}
var answer = 0
for (idx in 1 .. 4) {
if (gears[idx].first() == 0) continue
// S๊ทน์ผ ๋๋ง ์ ์ ์ถ๊ฐ
when(idx) {
1 -> answer += 1
2 -> answer += 2
3 -> answer += 4
4 -> answer += 8
}
}
print(answer)
}
'Algorithm > Algorithm (๋ฌธ์ ํ์ด)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 18870 ์ขํ ์์ถ (0) | 2022.11.01 |
---|---|
[๋ฐฑ์ค] 12100 2048 (Easy) (0) | 2022.10.28 |
[๋ฐฑ์ค] 2457 ๊ณต์ฃผ๋์ ์ ์ (0) | 2022.10.21 |
[๋ฐฑ์ค] 14499 ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ (0) | 2022.10.13 |
[๋ฐฑ์ค] 11559 Puyo Puyo (2) | 2022.10.13 |