結果

問題 No.3 ビットすごろく
ユーザー GBGB
提出日時 2018-04-16 13:22:26
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,430 bytes
コンパイル時間 2,893 ms
コンパイル使用メモリ 74,444 KB
実行使用メモリ 56,348 KB
最終ジャッジ日時 2023-09-09 10:52:17
合計ジャッジ時間 7,948 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,924 KB
testcase_01 AC 123 ms
55,604 KB
testcase_02 AC 122 ms
55,848 KB
testcase_03 AC 126 ms
56,196 KB
testcase_04 AC 125 ms
56,176 KB
testcase_05 AC 125 ms
55,456 KB
testcase_06 AC 124 ms
55,824 KB
testcase_07 AC 122 ms
55,540 KB
testcase_08 AC 123 ms
55,844 KB
testcase_09 AC 126 ms
56,012 KB
testcase_10 AC 132 ms
55,828 KB
testcase_11 AC 126 ms
55,976 KB
testcase_12 AC 123 ms
55,824 KB
testcase_13 AC 123 ms
55,652 KB
testcase_14 AC 132 ms
55,916 KB
testcase_15 AC 132 ms
56,000 KB
testcase_16 AC 132 ms
55,708 KB
testcase_17 AC 131 ms
55,828 KB
testcase_18 AC 123 ms
55,708 KB
testcase_19 AC 130 ms
55,880 KB
testcase_20 AC 121 ms
55,820 KB
testcase_21 AC 120 ms
55,716 KB
testcase_22 AC 130 ms
54,052 KB
testcase_23 AC 130 ms
55,800 KB
testcase_24 AC 132 ms
56,348 KB
testcase_25 AC 132 ms
55,948 KB
testcase_26 WA -
testcase_27 AC 123 ms
55,852 KB
testcase_28 AC 132 ms
55,940 KB
testcase_29 AC 125 ms
56,008 KB
testcase_30 AC 120 ms
55,428 KB
testcase_31 AC 121 ms
55,608 KB
testcase_32 AC 124 ms
55,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main{
  public static void main(String[] args) throws IOException{
    Scanner scan=new Scanner(System.in);

    int n=scan.nextInt();
    if(n==1){
      System.out.println(0);
      return;
    }

    Queue<Integer> queue=new ArrayDeque<Integer>();//空のキュー
    int[] dist=new int[n+1];//訪問したかどうかの判別する配列
    Arrays.fill(dist,Integer.MAX_VALUE);//初期化

    queue.offer(1);//キューに最初の1のマスを格納する
    dist[1]=1;//1のマスを訪問済みにする
    while(!queue.isEmpty()){
      int now=queue.poll();
      int bit=f(now);//bitが立っている数を数える

      //探索が完了したら出力して終了
      if(now+bit==n){
        System.out.println(dist[now]+1);
        return;
      }
      //未訪問で左に移動できる場合
      if(now-bit>0 && dist[now-bit]==Integer.MAX_VALUE){
        dist[now-bit]=dist[now]+1;
        queue.offer(now-bit);
      }
      //未訪問で右に移動できる場合
      if(now+bit<n && dist[now+bit]==Integer.MAX_VALUE){
        dist[now+bit]=dist[now]+1;//次のマスを訪問済みにする
        queue.offer(now+bit);//次のマスをキューに格納する
      }
    }
    System.out.println(-1);
  }
  static int f(int x){
    int count=0;
    while(x>0){
      if(x%2==1){
        count++;
      }
      x/=2;
    }
    return count;
  }
}
0