結果

問題 No.3 ビットすごろく
ユーザー kenji_shioyakenji_shioya
提出日時 2016-05-25 21:51:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 245 ms / 5,000 ms
コード長 1,713 bytes
コンパイル時間 4,101 ms
コンパイル使用メモリ 75,152 KB
実行使用メモリ 58,260 KB
最終ジャッジ日時 2023-09-13 23:58:47
合計ジャッジ時間 11,357 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,768 KB
testcase_01 AC 127 ms
55,776 KB
testcase_02 AC 126 ms
55,876 KB
testcase_03 AC 150 ms
56,120 KB
testcase_04 AC 141 ms
55,896 KB
testcase_05 AC 177 ms
55,884 KB
testcase_06 AC 154 ms
55,872 KB
testcase_07 AC 143 ms
55,888 KB
testcase_08 AC 164 ms
55,648 KB
testcase_09 AC 191 ms
57,792 KB
testcase_10 AC 220 ms
57,496 KB
testcase_11 AC 185 ms
55,764 KB
testcase_12 AC 176 ms
56,432 KB
testcase_13 AC 143 ms
55,928 KB
testcase_14 AC 203 ms
55,596 KB
testcase_15 AC 234 ms
57,824 KB
testcase_16 AC 237 ms
55,708 KB
testcase_17 AC 241 ms
57,960 KB
testcase_18 AC 160 ms
55,800 KB
testcase_19 AC 238 ms
58,068 KB
testcase_20 AC 147 ms
55,576 KB
testcase_21 AC 130 ms
55,484 KB
testcase_22 AC 207 ms
58,260 KB
testcase_23 AC 233 ms
58,240 KB
testcase_24 AC 245 ms
58,184 KB
testcase_25 AC 240 ms
58,076 KB
testcase_26 AC 133 ms
55,840 KB
testcase_27 AC 155 ms
55,892 KB
testcase_28 AC 227 ms
57,764 KB
testcase_29 AC 200 ms
55,780 KB
testcase_30 AC 137 ms
55,608 KB
testcase_31 AC 133 ms
55,716 KB
testcase_32 AC 187 ms
56,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Exercises16{
  public static void main (String[] args){

    Scanner sc = new Scanner(System.in);

    int goal = sc.nextInt();

    LinkedList<Block> queue = new LinkedList<Block>();

    ArrayList<Integer> beforeQueue = new ArrayList<Integer>();
    ArrayList<Integer> closed = new ArrayList<Integer>();

    boolean flag = false;

    closed.add(0);
    queue.offer(new Block(1, 1));

    while(queue.size() > 0){
      Block currentBlock;

      currentBlock = queue.poll();
      if (currentBlock.value == goal){
        System.out.println(currentBlock.count);
        flag = true;
        break;
      }

      Block forward = currentBlock.getForwardNum();
      Block back = currentBlock.getBackNum();
      if (beforeQueue.indexOf(forward.value) == -1 && forward.value <= goal){
        queue.offer(forward);
        beforeQueue.add(forward.value);
      }
      if (closed.indexOf(back.value) == -1){
        queue.offer(back);
        closed.add(back.value);
      }
    }
    if (queue.size() == 0 && flag == false){
      System.out.println(-1);
    }
  }
}

class Block {
  public int value;
  private int num;
  public int count;
  private int bitcount;

  Block (int newValue, int currentCount){
    value = newValue;
    num = newValue;
    count = currentCount;
    bitcount = 0;
  }

  public Block getForwardNum(){
    while (num != 0){
      if (num % 2 == 1){
        bitcount += 1;
      }
      num /= 2;
    }

    return new Block (value + bitcount, count + 1);
  }

  public Block getBackNum(){
    while (num != 0){
      if (num % 2 == 1){
        bitcount += 1;
      }
      num /= 2;
    }

    return new Block (value - bitcount, count + 1);
  }
}
0