結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2016-11-06 21:11:21 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 165 ms / 5,000 ms |
コード長 | 1,336 bytes |
コンパイル時間 | 2,213 ms |
コンパイル使用メモリ | 78,936 KB |
実行使用メモリ | 54,388 KB |
最終ジャッジ日時 | 2024-07-01 08:09:14 |
合計ジャッジ時間 | 7,932 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Here your code ! Scanner sc = new Scanner(System.in); int N = sc.nextInt(); Queue<Integer> q = new ArrayDeque<>(); String str; int[] mas = new int[N]; Arrays.fill(mas, -1); int now, next, prev; int count, result; mas[0] = 1; q.offer(1); while(!q.isEmpty()){ now = q.poll(); if(now == N){ //result = mas[N-1]; break; } str = Integer.toBinaryString(now); count = 0; for(char x: str.toCharArray()){ if(x == '1'){ count++; } } next = now + count; if(next <= N && mas[next-1] == -1){ mas[next-1] = mas[now-1] +1; q.offer(next); } prev = now - count; if(prev > 0 && mas[prev-1] == -1){ mas[prev-1] = mas[now-1] +1; q.offer(prev); } } /*for(int i=0; i<N; i++){ System.out.print(mas[i]+" "); } System.out.println("");*/ System.out.println(mas[N-1]); } }