結果

問題 No.3 ビットすごろく
ユーザー koba-e964
提出日時 2015-12-09 02:57:07
言語 Scala(Beta)
(3.6.2)
結果
AC  
実行時間 1,075 ms / 5,000 ms
コード長 755 bytes
コンパイル時間 11,364 ms
コンパイル使用メモリ 266,104 KB
実行使用メモリ 65,980 KB
最終ジャッジ日時 2024-07-01 07:41:36
合計ジャッジ時間 46,954 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
 * Reference: http://lizan.asia/blog/2012/12/11/scala-competitive/
 */

object Main extends App {
  import java.{util => ju}
  import scala.annotation._
  import scala.collection._
  import scala.collection.{mutable => mu}
  import scala.collection.JavaConverters._
  import scala.math._

  val sc = new ju.Scanner(System.in)

  val n = sc.nextInt
  val que = new mu.Queue[(Int, Int)]
  val inf = 0x3fffffff
  val dist = Array.fill(n + 1)(inf)
  que += 0 -> 1
  while (que.nonEmpty) {
    val (d, t) = que.dequeue
    if (d < dist(t)) {
      dist(t) = d
      val bc = Integer.bitCount(t)
      if (t - bc >= 1) que += (d + 1) -> (t - bc)
      if (t + bc <= n) que += (d + 1) -> (t + bc)
    }
  }
  println(if (dist(n) == inf) -1 else dist(n) + 1)
}
0