結果

問題 No.3 ビットすごろく
コンテスト
ユーザー sakuraigaku
提出日時 2019-05-20 00:40:06
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
TLE  
実行時間 -
コード長 2,011 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,444 ms
コンパイル使用メモリ 287,408 KB
実行使用メモリ 438,460 KB
最終ジャッジ日時 2026-03-09 17:02:52
合計ジャッジ時間 75,107 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27 TLE * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package main

import java.util
import scala.collection.mutable


object Main {

  def main(args: Array[String]): Unit = {

    var minCount: Int = 2147483647

    val sc = new java.util.Scanner(System.in)
    val i: Int = sc.nextInt()

//    val stack = mutable.Stack[Point]()
//    val ans: mutable.Set[Point] = mutable.Set[Point]()
//
//    stack.push(Point(1, 1, mutable.Set(1)))
//    while (!stack.isEmpty) {
//      val p = stack.pop()
//      if (p.now == i) {
//        ans += p
//        minCount = p.count
//      } else if (1 <= p.now && p.now <= i && p.count < minCount) {
//        val newPoint: Int = p.now.toBinaryString.replaceAll("0", "").length
//        if (!p.history(p.now + newPoint)) {
//          stack.push(Point(p.count + 1, p.now + newPoint, p.history += p.now))
//        }
//        if (!p.history(p.now - newPoint)) {
//          stack.push(Point(p.count + 1, p.now - newPoint, p.history += p.now))
//        }
//      }
//    }
//
//    if (ans.isEmpty) {
//      printf("-1")
//    } else {
//      printf(ans.seq.map(p => p.count).min.toString)
//    }

//    val queue = new util.ArrayDeque[Point]()
    
    var queue = new mutable.Queue[Point]
    val ans: mutable.Set[Point] = mutable.Set[Point]()

    queue += Point(1, 1, mutable.Set(1))
    while (!queue.isEmpty) {
      val p = queue.dequeue()
      if (p.now == i) {
        ans += p
        minCount = p.count
      } else if (1 <= p.now && p.now <= i && p.count < minCount) {
        val newPoint: Int = p.now.toBinaryString.replaceAll("0", "").length
        if (!p.history(p.now + newPoint)) {
          queue += Point(p.count + 1, p.now + newPoint, p.history += p.now)
        }
        if (!p.history(p.now - newPoint)) {
          queue += Point(p.count + 1, p.now - newPoint, p.history += p.now)
        }
      }
    }

    if (ans.isEmpty) {
      printf("-1")
    } else {
      printf(ans.seq.map(p => p.count).min.toString)
    }

  }

}

case class Point(count: Int, now: Int, history: mutable.Set[Int])
0