fork download
  1. import java.util.concurrent.atomic.AtomicInteger
  2.  
  3. import scala.Vector
  4. import scala.collection.immutable.VectorBuilder
  5. import scala.util.Random
  6.  
  7. object Labyrinth {
  8. val LABYRINTH =
  9. "0 0 0 0 0 0 0 0 0 0 0 0" +
  10. "/ / / / / / / / / / / /" +
  11. "0/2/3/3/3/3/3/3/3/3/2/0" +
  12. "/ / / / / / / / / / / /" +
  13. "0/3/4/4/4/4/4/4/4/4/3/0" +
  14. "/ / / / / / / / / / / /" +
  15. "0/3/4/4/4/4/4/4/4/4/3/0" +
  16. "/ / / / / / / / / / / /" +
  17. "0/3/4/4/4/4/4/4/4/4/3/0" +
  18. "/ / / / / / / / / / / /" +
  19. "0/3/4/4/4/4/4/4/4/4/3/0" +
  20. "/ / / / / / / / / / / /" +
  21. "0/3/4/4/4/4/4/4/4/4/3/0" +
  22. "/ / / / / / / / / / / /" +
  23. "0/3/4/4/4/4/4/4/4/4/3/0" +
  24. "/ / / / / / / / / / / /" +
  25. "0/3/4/4/4/4/4/4/4/4/3/0" +
  26. "/ / / / / / / / / / / /" +
  27. "0/3/4/4/4/4/4/4/4/4/3/0" +
  28. "/ / / / / / / / / / / /" +
  29. "0/2/3/3/3/3/3/3/3/3/2/0" +
  30. "/ / / / / / / / / / / /" +
  31. "0 0 0 0 0 0 0 0 0 0 0 0"
  32.  
  33. val WALL_X = 1
  34. val WALL_Y = 23
  35. val BEEPER_X = 2 * WALL_X
  36. val BEEPER_Y = 2 * WALL_Y
  37.  
  38. val START_POSITION = BEEPER_Y + BEEPER_X
  39.  
  40. val WALLS0 = 0x01e9ff17
  41. val WALLS1 = 0xe9ff1701
  42. val WALLS2 = 0xff1701e9
  43. val WALLS3 = 0x1701e9ff
  44.  
  45. val BEEPERS0 = 0x02d2fe2e
  46. val BEEPERS1 = 0xd2fe2e02
  47. val BEEPERS2 = 0xfe2e02d2
  48. val BEEPERS3 = 0x2e02d2fe
  49.  
  50. val id = new AtomicInteger(0)
  51.  
  52. def walk(): Vector[Long] = {
  53. case class Solution(lab: Array[Int], pos: Int) extends Exception
  54.  
  55. val lab = LABYRINTH.map(_.toInt).toArray
  56. val rng = new Random(id.getAndIncrement())
  57.  
  58. def causesPartition(pos: Int, dir: Int): Boolean = {
  59. (lab(pos + (BEEPERS0 << dir >> 24)) <= '0') &&
  60. (lab(pos + (BEEPERS1 << dir >> 24)) > '0') &&
  61. (lab(pos + (BEEPERS3 << dir >> 24)) > '0')
  62. }
  63.  
  64. def destinationOpen(pos: Int, dir: Int, n: Int) {
  65. if (causesPartition(pos, dir)) return
  66.  
  67. val before = lab(pos)
  68. lab(pos) = '0'
  69. if (n <= 1) throw Solution(lab, pos)
  70.  
  71. val a = pos + BEEPER_X
  72. val b = pos - BEEPER_Y
  73. val c = pos - BEEPER_X
  74. val d = pos + BEEPER_Y
  75.  
  76. lab(a) -= 1
  77. lab(b) -= 1
  78. lab(c) -= 1
  79. lab(d) -= 1
  80.  
  81. var ones = 0
  82. if (lab(a) == '1') ones += 1
  83. if (lab(b) == '1') ones += 1
  84. if (lab(c) == '1') ones += 1
  85. if (lab(d) == '1') ones += 1
  86.  
  87. var beepers = 0
  88. var walls = 0
  89. val r = rng.nextInt(4)
  90. r match {
  91. case 0 =>
  92. beepers = BEEPERS0
  93. walls = WALLS0
  94. case 1 =>
  95. beepers = BEEPERS1
  96. walls = WALLS1
  97. case 2 =>
  98. beepers = BEEPERS2
  99. walls = WALLS2
  100. case 3 =>
  101. beepers = BEEPERS3
  102. walls = WALLS3
  103. }
  104. var dir = r * 8
  105. var n = 4
  106. if (ones == 0) {
  107. while (n > 0) {
  108. val beeper = pos + (beepers >> 24)
  109. if (lab(beeper) >= '0') {
  110. val wall = pos + (walls >> 24)
  111. lab(wall) = ' '
  112. destinationOpen(beeper, (dir + 8) & 31, n - 1)
  113. lab(wall) = '/'
  114. }
  115. beepers = beepers << 8 | beepers >>> 24;
  116. walls = walls << 8 | walls >>> 24;
  117. n -= 1
  118. }
  119. } else if (ones == 2) {
  120. while (n > 0) {
  121. val beeper = pos + (beepers >> 24)
  122. if (lab(beeper) == '1') {
  123. val wall = pos + (walls >> 24)
  124. lab(wall) = ' '
  125. destinationOpen(beeper, (dir + 8) & 31, n - 1)
  126. lab(wall) = '/'
  127. }
  128. beepers = beepers << 8 | beepers >>> 24;
  129. walls = walls << 8 | walls >>> 24;
  130. n -= 1
  131. }
  132. } else if (ones == 1) {
  133. while (n > 0) {
  134. val beeper = pos + (beepers >> 24)
  135. if (lab(beeper) >= '0') {
  136. val wall = pos + (walls >> 24)
  137. if (lab(beeper) == '1') {
  138. lab(wall) = ' '
  139. destinationOpen(beeper, (dir + 8) & 31, n - 1)
  140. lab(wall) = '/'
  141. } else {
  142. lab(wall) = ' '
  143. destinationFound(beeper, (dir + 8) & 31, n - 1)
  144. lab(wall) = '/'
  145. }
  146. }
  147. beepers = beepers << 8 | beepers >>> 24;
  148. walls = walls << 8 | walls >>> 24;
  149. n -= 1
  150. }
  151. }
  152.  
  153. lab(a) += 1
  154. lab(b) += 1
  155. lab(c) += 1
  156. lab(d) += 1
  157.  
  158. lab(pos) = before
  159. }
  160.  
  161. def destinationFound(pos: Int, dir: Int, n: Int) {
  162. if (causesPartition(pos, dir)) return
  163.  
  164. val before = lab(pos)
  165. lab(pos) = '0'
  166. if (n <= 1) throw Solution(lab, pos)
  167.  
  168. val a = pos + BEEPER_X
  169. val b = pos - BEEPER_Y
  170. val c = pos - BEEPER_X
  171. val d = pos + BEEPER_Y
  172.  
  173. lab(a) -= 1
  174. lab(b) -= 1
  175. lab(c) -= 1
  176. lab(d) -= 1
  177.  
  178. var ones = 0
  179. if (lab(a) == '1') ones += 1
  180. if (lab(b) == '1') ones += 1
  181. if (lab(c) == '1') ones += 1
  182. if (lab(d) == '1') ones += 1
  183.  
  184. var beepers = 0
  185. var walls = 0
  186. val r = rng.nextInt(4)
  187. r match {
  188. case 0 =>
  189. beepers = BEEPERS0
  190. walls = WALLS0
  191. case 1 =>
  192. beepers = BEEPERS1
  193. walls = WALLS1
  194. case 2 =>
  195. beepers = BEEPERS2
  196. walls = WALLS2
  197. case 3 =>
  198. beepers = BEEPERS3
  199. walls = WALLS3
  200. }
  201. var dir = r * 8
  202. var n = 4
  203. if (ones == 0) {
  204. while (n > 0) {
  205. val beeper = pos + (beepers >> 24)
  206. if (lab(beeper) >= '0') {
  207. val wall = pos + (walls >> 24)
  208. lab(wall) = ' '
  209. destinationFound(beeper, (dir + 8) & 31, n - 1)
  210. lab(wall) = '/'
  211. }
  212. beepers = beepers << 8 | beepers >>> 24;
  213. walls = walls << 8 | walls >>> 24;
  214. n -= 1
  215. }
  216. } else if ((ones == 1) && (n == 2)) {
  217. while (n > 0) {
  218. val beeper = pos + (beepers >> 24)
  219. if (lab(beeper) == '1') {
  220. val wall = pos + (walls >> 24)
  221. lab(wall) = ' '
  222. destinationFound(beeper, (dir + 8) & 31, n - 1)
  223. lab(wall) = '/'
  224. }
  225. beepers = beepers << 8 | beepers >>> 24;
  226. walls = walls << 8 | walls >>> 24;
  227. n -= 1
  228. }
  229. }
  230. /*
  231.   if (ones == 0) {
  232.   while (i < j) {
  233.   val beeper = pos + BEEPERS(i)
  234.   if (lab(beeper) >= '0') {
  235.   val wall = pos + WALLS(i)
  236.   lab(wall) = ' '
  237.   destinationFound(beeper, i & 3, n - 1)
  238.   lab(wall) = '/'
  239.   }
  240.   i += 1
  241.   }
  242.   } else if ((ones == 1) && (n == 2)) {
  243.   while (i < j) {
  244.   val beeper = pos + BEEPERS(i)
  245.   if (lab(beeper) == '1') {
  246.   val wall = pos + WALLS(i)
  247.   lab(wall) = ' '
  248.   destinationFound(beeper, i & 3, n - 1)
  249.   lab(wall) = '/'
  250.   }
  251.   i += 1
  252.   }
  253.   }
  254. */
  255. lab(a) += 1
  256. lab(b) += 1
  257. lab(c) += 1
  258. lab(d) += 1
  259.  
  260. lab(pos) = before
  261. }
  262.  
  263. try {
  264. destinationOpen(START_POSITION, 0, 95)
  265. Vector(10)
  266. } catch {
  267. case Solution(lab, target) =>
  268. val vb = new VectorBuilder[Long]
  269.  
  270. for (y <- 1 to 10) {
  271. var line = 0L
  272. for (x <- 1 to 10) {
  273. val pos = y * BEEPER_Y + x * BEEPER_X
  274. val a = lab(pos + WALL_X) & 1
  275. val b = lab(pos - WALL_Y) & 2
  276. val c = lab(pos - WALL_X) & 4
  277. val d = lab(pos + WALL_Y) & 8
  278. line = (line << 4) | a | b | c | d
  279. }
  280. vb += (line << 24)
  281. }
  282. vb.result
  283. }
  284. }
  285. }
  286.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/opt/scala/bin/scalac: line 50: /dev/null: Permission denied
Main.scala:65: error: forward reference extends over definition of value before
      if (causesPartition(pos, dir)) return
                               ^
Main.scala:69: error: forward reference extends over definition of value a
      if (n <= 1) throw Solution(lab, pos)
          ^
Main.scala:162: error: forward reference extends over definition of value before
      if (causesPartition(pos, dir)) return
                               ^
Main.scala:166: error: forward reference extends over definition of value a
      if (n <= 1) throw Solution(lab, pos)
          ^
four errors found
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty