結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2022-05-17 16:43:22 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 61 ms / 5,000 ms |
コード長 | 1,213 bytes |
コンパイル時間 | 4,675 ms |
コンパイル使用メモリ | 78,828 KB |
実行使用メモリ | 37,120 KB |
最終ジャッジ日時 | 2024-09-15 11:27:05 |
合計ジャッジ時間 | 7,076 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Queue; public class No3 { public static void main(String[] args) throws IOException{ //ビットすごろく int N = Integer.parseInt(readStr()[0]) , d = 0 , pl , mi; int[] root = new int[N + 1]; root[1] = 1; Queue<Integer> qu = new ArrayDeque<>(); qu.add(1); while(qu.size() > 0) { d = qu.poll(); pl = d + Integer.bitCount(d); mi = d - Integer.bitCount(d); if(pl <= N && (root[pl] == 0 || root[pl] > root[d] + 1)) { root[pl] = root[d] + 1; if(pl < N) { qu.add(pl); } } if(mi >= 1 && (root[mi] == 0 || root[mi] > root[d] + 1)) { root[mi] = root[d] + 1; qu.add(mi); } } System.out.println(root[N] == 0 ? -1 : root[N]); } public static String[] readStr() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> list = new ArrayList<>(); do { list.add(br.readLine()); }while(br.ready()); br.close(); String[] text = new String[list.size()]; list.toArray(text); return text; } }