結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
YamaKasa
|
| 提出日時 | 2018-06-08 02:53:45 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 183 ms / 5,000 ms |
| コード長 | 1,416 bytes |
| コンパイル時間 | 2,180 ms |
| コンパイル使用メモリ | 77,728 KB |
| 実行使用メモリ | 57,464 KB |
| 最終ジャッジ日時 | 2024-07-01 09:03:05 |
| 合計ジャッジ時間 | 8,484 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
scan.close();
// マスの状態 0: 訪れていない 1: 訪れた
int []color = new int[N];
Arrays.fill(color, 0);
color[0] = 1;
// 次に訪問すべきマス
Queue<Integer> queue = new ArrayDeque<Integer>();
queue.add(1);
// 1からの距離
int []d = new int[N];
Arrays.fill(d, 0);
d[0] = 1;
while(queue.size() > 0){
// 訪問しているマス
int k = queue.poll();
// 移動可能な距離
int t = bitNum(k);
// 移動するマス
int a1 = k + t;
int a2 = k - t;
//System.out.println(a1 + " " + a2);
if(a1 <= N) {
if(color[a1 - 1] == 0){
color[a1 - 1] = 1;
d[a1 - 1] = d[k - 1] + 1;
queue.add(a1);
}
}
if(a2 >= 2) {
if(color[a2 - 1] == 0) {
color[a2 - 1] = 1;
d[a2 - 1] = d[k - 1] + 1;
queue.add(a2);
}
}
if(color[N - 1] == 1) {
System.out.println(d[N - 1]);
System.exit(0);
}
}
System.out.println(-1);
}
public static int bitNum(int a) {
int cnt = 0;
String binary = Integer.toBinaryString(a);
int l = binary.length();
for(int j = 0; j < l; j++) {
if(binary.substring(j, j+1).equals("1")) {
cnt ++;
}
}
return cnt;
}
}
YamaKasa