結果

問題 No.3 ビットすごろく
ユーザー kenji_shioyakenji_shioya
提出日時 2016-05-25 18:29:27
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,586 bytes
コンパイル時間 3,944 ms
コンパイル使用メモリ 78,240 KB
実行使用メモリ 52,068 KB
最終ジャッジ日時 2024-04-16 16:41:32
合計ジャッジ時間 11,079 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
41,252 KB
testcase_01 AC 136 ms
41,336 KB
testcase_02 AC 126 ms
40,100 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
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 #

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> closed = new ArrayList<Integer>();

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

    while(true){
      Block currentBlock;

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

      Block forward = currentBlock.getForwardNum();
      Block back = currentBlock.getBackNum();

      queue.offer(forward);
      if (closed.indexOf(back.value) == -1){
        queue.offer(back);
      }


      int lastNumInClosed = closed.get((closed.size() - 1));
      //System.out.println(lastNumInClosed);
      if (lastNumInClosed > goal * 2){
      System.out.println(-1);
      break;
      }
    }
  }
}

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