結果
問題 | No.3 ビットすごろく |
ユーザー | aaaaasatori |
提出日時 | 2018-02-18 13:39:40 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 146 ms / 5,000 ms |
コード長 | 906 bytes |
コンパイル時間 | 3,401 ms |
コンパイル使用メモリ | 78,584 KB |
実行使用メモリ | 54,456 KB |
最終ジャッジ日時 | 2024-07-01 08:56:10 |
合計ジャッジ時間 | 9,169 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class No3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int hosuu[] = new int[N+1]; hosuu[1] = 1; Queue<Integer> queue = new LinkedList(); queue.offer(1); while(true) { Integer i = queue.poll(); if(i == null) { break; } if(i - bitcount(i) > 0 && hosuu[i - bitcount(i)] == 0){ hosuu[i - bitcount(i)] = hosuu[i] + 1; queue.offer(i - bitcount(i)); } if(i + bitcount(i) <= N && hosuu[i + bitcount(i)] == 0) { hosuu[i + bitcount(i)] = hosuu[i] + 1; queue.offer(i + bitcount(i)); } } if(hosuu[N] == 0) { System.out.println(-1); }else { System.out.println(hosuu[N]); } } public static int bitcount(int x) { int bit = 0; while(x != 0) { bit += x % 2; x /= 2; } return bit; } }