diff --git a/scala-leetcode/src/main/scala/io/github/poorguy/OpenTheLock.scala b/scala-leetcode/src/main/scala/io/github/poorguy/OpenTheLock.scala new file mode 100644 index 0000000000000000000000000000000000000000..ed444dce72919f8ea2ab5ac68013fc41cd1388c3 --- /dev/null +++ b/scala-leetcode/src/main/scala/io/github/poorguy/OpenTheLock.scala @@ -0,0 +1,57 @@ +package io.github.poorguy + +import io.github.poorguy.`trait`.{Bfs_, Queue_} + +import scala.collection.mutable + +object OpenTheLock extends App with Queue_ with Bfs_ { + def openLock(deadends: Array[String], target: String): Int = { + val origin = "0000" + if (origin.equals(target)) { + return 0 + } + + val deadSet = deadends.toSet + val directions = List.range(0, 8) + var depth = 0 + var sameDepthCount = 0 + val queue = mutable.Queue[String]("0000") + while (queue.nonEmpty) { + if (sameDepthCount == 0) depth = depth + 1 + else sameDepthCount -= 1 + + val value = queue.dequeue() + for (direct <- directions) { + val newVal = tune(value, deadSet, direct) + if (deadSet.contains(newVal)) { + + } else if (newVal.equals(target)) { + return depth + } else { + queue.enqueue(newVal) + sameDepthCount = sameDepthCount + 1 + } + } + } + -1 + } + + def tune(origin: String, deadSet: Set[String], direction: Int): String = { + val upDown = if (direction % 2 == 0) 1 else -1 + val index = origin.length() - 1 - direction / 2 + val value = origin.charAt(index).toString.toInt + val originChars = origin.toCharArray + + if (upDown > 0 && value == 9) { + originChars(index) = '0' + } else if (upDown < 0 && value == 0) { + originChars(index) = '9' + } else { + originChars(index) = (value + upDown).toString.charAt(0) + } + originChars.mkString + } + + val list = Array("0201", "0101", "0102", "1212", "2002") + print(openLock(list, "0202")) +} diff --git a/scala-leetcode/src/main/scala/io/github/poorguy/trait/Bfs_.scala b/scala-leetcode/src/main/scala/io/github/poorguy/trait/Bfs_.scala new file mode 100644 index 0000000000000000000000000000000000000000..7592581fd44fae724148da0090a4e827e1374527 --- /dev/null +++ b/scala-leetcode/src/main/scala/io/github/poorguy/trait/Bfs_.scala @@ -0,0 +1,8 @@ +package io.github.poorguy.`trait` + +/** + * solution with BFS algorithm + */ +trait Bfs_ { + +} diff --git a/scala-leetcode/src/main/scala/io/github/poorguy/trait/Queue_.scala b/scala-leetcode/src/main/scala/io/github/poorguy/trait/Queue_.scala new file mode 100644 index 0000000000000000000000000000000000000000..21546715f017e8a09f525e5720641c779fae2624 --- /dev/null +++ b/scala-leetcode/src/main/scala/io/github/poorguy/trait/Queue_.scala @@ -0,0 +1,8 @@ +package io.github.poorguy.`trait` + +/** + * Solution with Queue + */ +trait Queue_ { + +} diff --git a/scala-leetcode/src/main/scala/io/github/poorguy/trait/Stack_.scala b/scala-leetcode/src/main/scala/io/github/poorguy/trait/Stack_.scala new file mode 100644 index 0000000000000000000000000000000000000000..3e1f0d149cbb36d5fb8f10539b62c72ce84f4dc2 --- /dev/null +++ b/scala-leetcode/src/main/scala/io/github/poorguy/trait/Stack_.scala @@ -0,0 +1,8 @@ +package io.github.poorguy.`trait` + +/** + * Solution with Stack + */ +trait Stack_ { + +}