結果

問題 No.3 ビットすごろく
ユーザー kusagame12kusagame12
提出日時 2023-11-06 19:32:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,284 ms / 5,000 ms
コード長 1,718 bytes
コンパイル時間 2,516 ms
コンパイル使用メモリ 79,208 KB
実行使用メモリ 93,940 KB
最終ジャッジ日時 2023-11-06 19:33:22
合計ジャッジ時間 23,000 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
56,296 KB
testcase_01 AC 124 ms
57,752 KB
testcase_02 AC 124 ms
57,568 KB
testcase_03 AC 141 ms
59,536 KB
testcase_04 AC 133 ms
57,632 KB
testcase_05 AC 204 ms
60,140 KB
testcase_06 AC 142 ms
59,304 KB
testcase_07 AC 126 ms
56,324 KB
testcase_08 AC 179 ms
60,124 KB
testcase_09 AC 304 ms
67,752 KB
testcase_10 AC 731 ms
69,748 KB
testcase_11 AC 235 ms
62,524 KB
testcase_12 AC 187 ms
60,132 KB
testcase_13 AC 139 ms
59,684 KB
testcase_14 AC 596 ms
69,924 KB
testcase_15 AC 1,743 ms
93,940 KB
testcase_16 AC 836 ms
69,892 KB
testcase_17 AC 1,632 ms
89,340 KB
testcase_18 AC 137 ms
59,756 KB
testcase_19 AC 2,284 ms
93,496 KB
testcase_20 AC 119 ms
56,328 KB
testcase_21 AC 125 ms
57,748 KB
testcase_22 AC 615 ms
69,732 KB
testcase_23 AC 2,280 ms
93,492 KB
testcase_24 AC 2,264 ms
93,464 KB
testcase_25 AC 1,714 ms
93,908 KB
testcase_26 AC 124 ms
57,276 KB
testcase_27 AC 140 ms
59,680 KB
testcase_28 AC 779 ms
69,888 KB
testcase_29 AC 253 ms
64,596 KB
testcase_30 AC 125 ms
57,632 KB
testcase_31 AC 124 ms
57,588 KB
testcase_32 AC 238 ms
62,612 KB
権限があれば一括ダウンロードができます

ソースコード

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;
        for(int i = 0 ; q.size() > 0 ; i++){
            turn++;
            while(q.size() > 0){
                
                int v = q.pop();

                if(v == n){
                    flag = true;
                    break;
                }
                
                seen[v] = true;
                
                int left = v - getBitCount(v);
                int right = v + getBitCount(v);
                
                if(left > 0 && !seen[left]){
                    next_q.push(left);
                }
                if(right <= n && !seen[right]){
                    next_q.push(right);
                }
                
            }
            if(flag){
                break;
            }
            q.addAll(next_q);
            next_q.clear();
        }
        
        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