結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2016-03-05 20:16:43 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,184 bytes |
コンパイル時間 | 2,059 ms |
コンパイル使用メモリ | 83,852 KB |
実行使用メモリ | 58,424 KB |
最終ジャッジ日時 | 2024-09-24 14:26:58 |
合計ジャッジ時間 | 10,294 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | WA * 33 |
ソースコード
import java.util.ArrayDeque; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] min = new int[N+1]; min[1] = 1; Queue<Integer> q = new ArrayDeque<>(); q.add(1); while (!q.isEmpty()) { int x = q.poll(); if (x == N) { break; } int v1 = x + count1(x); int v2 = x - count1(x); if (v1 > 0 && v1 <= N && min[v1] == 0) { q.add(v1); min[v1] = min[x] + 1; } if (v2 > 0 && v2 <= N && min[v2] == 0) { q.add(v2); min[v2] = min[x] + 1; } } if (min[N] == 0) { System.out.println(-1); } else { System.out.println(min[N]); } for (int i=0; i<=N; i++) { System.out.print(min[i] + " "); } System.out.println(); sc.close(); } static int count1(int x) { int tmp = x; int cnt = 0; while (tmp != 0) { cnt += tmp & 1; tmp >>= 1; } return cnt; } }