Skip to content

Commit 49945cc

Browse files
committed
톱니바퀴 해결
1 parent d0f913e commit 49945cc

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/4week/acw/톱니바퀴.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ const val RIGHT = 5
88
const val LEFT = 1
99

1010
class `acw톱니바퀴` {
11+
1112
var wheel = Array(4) { 0 }
1213
var visit = Array(4) { false }
1314
var turn = arrayListOf<Pair<Int, Int>>()
1415

1516

1617
fun addTurn(w: Int, turnD: Int) {
18+
//dfs로 들어가면서 회전해야할 것들 turn에 추가해주기
1719

1820

1921
var wheelNowRight = if (wheel[w] and (1 shl RIGHT) != 0) 1 else 0
2022
var wheelNowLeft = if (wheel[w] and (1 shl LEFT) != 0) 1 else 0
21-
println("$w $wheelNowLeft $wheelNowRight")
2223

23-
if (w - 1 > 0) {
24+
if (w - 1 >= 0) {
2425
val leftWheelsRight = if (wheel[w - 1] and (1 shl RIGHT) != 0) 1 else 0
26+
2527
if (wheelNowLeft != leftWheelsRight && !visit[w - 1]) {
2628
visit[w - 1] = true
2729
turn.add(Pair(w - 1, turnD * -1))
@@ -30,7 +32,7 @@ class `acw톱니바퀴` {
3032
}
3133

3234
if (w + 1 < 4) {
33-
val rightWheelsLeft = if (wheel[w - 1] and (1 shl LEFT) != 0) 1 else 0
35+
val rightWheelsLeft = if (wheel[w + 1] and (1 shl LEFT) != 0) 1 else 0
3436
if (wheelNowRight != rightWheelsLeft && !visit[w + 1]) {
3537
visit[w + 1] = true
3638
turn.add(Pair(w + 1, turnD * -1))
@@ -50,7 +52,7 @@ class `acw톱니바퀴` {
5052
//반시계
5153
val last = wheel[w] and (1 shl 7)
5254

53-
wheel[w] = wheel[w] shl 1
55+
wheel[w] = (wheel[w] shl 1) and 255
5456
if (last != 0) {
5557
wheel[w]++
5658
}
@@ -80,31 +82,27 @@ class `acw톱니바퀴` {
8082
for (i in input) {
8183
num += (i.digitToInt() * m)
8284
m /= 2
83-
}
85+
}//숫자로 바꿔서 저장
8486
wheel[it] = num
8587

8688
}
8789

8890
val turnCnt = readln().toInt()
8991
repeat(turnCnt) {
9092
val (w, d) = readln().split(" ").map { it.toInt() }
91-
turn.add(Pair(w, d))
92-
visit[w] = true
93+
turn.add(Pair(w - 1, d))
94+
visit[w - 1] = true
9395

9496

95-
addTurn(w, d)
96-
println(turn)
97-
turnWheel()
98-
for (i in wheel) {
99-
println(i)
100-
}
101-
visit = Array(4) { false }
97+
addTurn(w - 1, d)//회전해야할 것들 turn List에 추가해주기
98+
turnWheel()// turn List에서 빼서 회전시켜주기
99+
visit = Array(4) { false } // visit은 다시 사용해야함으로 다시 false로 초기화
102100
}
103101

104102

105103
var answer = 0
106104
for (i in 0 until 4) {
107-
if ((wheel[i] and (1 shr 7)) == 0) {
105+
if ((wheel[i] and (1 shl 7)) != 0) {
108106
answer += (2.0).pow(i).toInt()
109107
}
110108
}

0 commit comments

Comments
 (0)