結果

問題 No.3 ビットすごろく
ユーザー sakuraigakusakuraigaku
提出日時 2019-05-20 00:40:06
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 2,011 bytes
コンパイル時間 9,903 ms
コンパイル使用メモリ 256,872 KB
実行使用メモリ 75,684 KB
最終ジャッジ日時 2023-09-14 18:31:54
合計ジャッジ時間 33,439 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,020 ms
63,928 KB
testcase_01 AC 1,013 ms
63,660 KB
testcase_02 AC 990 ms
63,464 KB
testcase_03 AC 1,257 ms
64,080 KB
testcase_04 AC 1,255 ms
64,428 KB
testcase_05 AC 1,708 ms
66,796 KB
testcase_06 AC 1,270 ms
64,400 KB
testcase_07 AC 1,249 ms
64,452 KB
testcase_08 AC 1,570 ms
66,960 KB
testcase_09 AC 4,057 ms
72,972 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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