結果

問題 No.3 ビットすごろく
ユーザー kusagame12kusagame12
提出日時 2023-11-06 19:26:03
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,464 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 78,816 KB
実行使用メモリ 57,828 KB
最終ジャッジ日時 2023-11-06 19:26:14
合計ジャッジ時間 8,812 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
57,540 KB
testcase_01 AC 132 ms
57,472 KB
testcase_02 AC 133 ms
57,728 KB
testcase_03 AC 140 ms
57,744 KB
testcase_04 AC 135 ms
57,440 KB
testcase_05 AC 144 ms
57,248 KB
testcase_06 AC 136 ms
55,764 KB
testcase_07 AC 138 ms
57,260 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 135 ms
57,568 KB
testcase_13 AC 139 ms
57,164 KB
testcase_14 AC 144 ms
57,552 KB
testcase_15 AC 140 ms
57,328 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 135 ms
57,596 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 136 ms
57,544 KB
testcase_22 AC 142 ms
57,572 KB
testcase_23 AC 143 ms
57,560 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 137 ms
57,584 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 135 ms
57,560 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        boolean[] seen = new boolean[n+1];
        seen[0] = true;
        
        LinkedList<Integer> q = new LinkedList<>();
        LinkedList<Integer> next_q = new LinkedList<>();
        q.push(1);
        
        int turn = 0;
        boolean flag = false;
        while(q.size() > 0){
            
            int v = q.pop();
            turn++;
            
            if(v == n){
                flag = true;
                break;
            }
            
            seen[v] = true;
            
            int left = v - getBitCount(v);
            int right = v + getBitCount(v);
            
            if(left > 0 && !seen[left]){
                q.push(left);
            }
            if(right <= n && !seen[right]){
                q.push(right);
            }
            
        }
        
        System.out.println(flag ? turn : -1);
        
    }
    
    private static int getBitCount(int bits){
        bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
        bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
        bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
        bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
        
        return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
    }
   
}

0