結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2017-12-05 10:15:37 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 146 ms / 5,000 ms |
コード長 | 967 bytes |
コンパイル時間 | 3,572 ms |
コンパイル使用メモリ | 78,960 KB |
実行使用メモリ | 54,492 KB |
最終ジャッジ日時 | 2024-07-01 08:51:43 |
合計ジャッジ時間 | 9,250 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import java.util.ArrayDeque; import java.util.Arrays; 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(); boolean [] boo = new boolean[n]; int []m = new int[n]; Arrays.fill(m, Integer.MAX_VALUE); for(int i=0;i<n;i++) { boo[i]=false; } Queue<Integer> q = new ArrayDeque<Integer>(); boo[0]=true; q.add(1); m[0]=1; while(!q.isEmpty()) { int now = q.poll(); int bitcnt = Integer.bitCount(now); int b = now-bitcnt; int f = now+bitcnt; if(b>0) { m[b-1]=Math.min(m[b-1],m[now-1]+1); if(!boo[b-1]) { q.add(b); } boo[b-1]=true; } if(f<=n) { m[f-1]=Math.min(m[f-1],m[now-1]+1); if(!boo[f-1]) { q.add(f); } boo[f-1]=true; } } if(m[n-1]==Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(m[n-1]); } sc.close(); } }