結果

問題 No.3 ビットすごろく
ユーザー jp_stejp_ste
提出日時 2016-02-12 17:05:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 1,283 bytes
コンパイル時間 2,759 ms
コンパイル使用メモリ 75,384 KB
実行使用メモリ 56,460 KB
最終ジャッジ日時 2023-09-13 23:44:05
合計ジャッジ時間 8,138 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
53,724 KB
testcase_01 AC 126 ms
55,480 KB
testcase_02 AC 126 ms
55,796 KB
testcase_03 AC 128 ms
55,664 KB
testcase_04 AC 128 ms
56,012 KB
testcase_05 AC 135 ms
55,928 KB
testcase_06 AC 130 ms
55,968 KB
testcase_07 AC 130 ms
55,484 KB
testcase_08 AC 130 ms
55,708 KB
testcase_09 AC 134 ms
55,976 KB
testcase_10 AC 136 ms
55,956 KB
testcase_11 AC 135 ms
55,484 KB
testcase_12 AC 129 ms
55,932 KB
testcase_13 AC 129 ms
56,048 KB
testcase_14 AC 134 ms
55,704 KB
testcase_15 AC 136 ms
55,776 KB
testcase_16 AC 136 ms
55,832 KB
testcase_17 AC 134 ms
56,088 KB
testcase_18 AC 128 ms
56,088 KB
testcase_19 AC 137 ms
55,512 KB
testcase_20 AC 128 ms
55,476 KB
testcase_21 AC 126 ms
55,876 KB
testcase_22 AC 135 ms
56,460 KB
testcase_23 AC 134 ms
55,776 KB
testcase_24 AC 135 ms
55,848 KB
testcase_25 AC 137 ms
56,192 KB
testcase_26 AC 128 ms
55,952 KB
testcase_27 AC 130 ms
55,664 KB
testcase_28 AC 134 ms
55,824 KB
testcase_29 AC 134 ms
55,768 KB
testcase_30 AC 126 ms
55,836 KB
testcase_31 AC 124 ms
55,768 KB
testcase_32 AC 128 ms
55,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        scan.close();
        boolean flag[] = new boolean[N+1];
        int ans = -1;
        
        Queue<Node> q = new LinkedList<>();
        q.add(new Node(1,1));
        
        while(!q.isEmpty()) {
            Node v = q.poll();
            
            if(v.number == N) {
                ans = v.steps;
                break;
            }
            
            if(flag[v.number]) continue;;
            flag[v.number] = true;
            
            int nextRight = v.number + (Integer.bitCount(v.number));
            if(nextRight <= N && !flag[nextRight]) { 
                q.add(new Node(nextRight, v.steps+1));
            }
            
            int nextLeft = v.number - (Integer.bitCount(v.number));
            if(nextLeft >= 1 && !flag[nextLeft]) { 
                q.add(new Node(nextLeft, v.steps+1));
            }
        }
        
        System.out.println(ans);
    }
}

class Node {
    int number;
    int steps;
    Node(int number , int steps) {
        this.number = number;
        this.steps = steps;
    }
}
0