結果

問題 No.3 ビットすごろく
ユーザー jp_stejp_ste
提出日時 2016-02-12 04:02:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,764 ms / 5,000 ms
コード長 1,088 bytes
コンパイル時間 6,303 ms
コンパイル使用メモリ 75,384 KB
実行使用メモリ 100,116 KB
最終ジャッジ日時 2023-09-13 23:43:08
合計ジャッジ時間 19,157 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,512 KB
testcase_01 AC 126 ms
56,156 KB
testcase_02 AC 126 ms
55,896 KB
testcase_03 AC 142 ms
57,852 KB
testcase_04 AC 134 ms
55,896 KB
testcase_05 AC 170 ms
58,356 KB
testcase_06 AC 141 ms
58,216 KB
testcase_07 AC 138 ms
55,956 KB
testcase_08 AC 165 ms
58,504 KB
testcase_09 AC 278 ms
66,040 KB
testcase_10 AC 572 ms
68,296 KB
testcase_11 AC 210 ms
62,564 KB
testcase_12 AC 171 ms
58,400 KB
testcase_13 AC 139 ms
57,812 KB
testcase_14 AC 471 ms
70,088 KB
testcase_15 AC 1,400 ms
89,580 KB
testcase_16 AC 650 ms
67,820 KB
testcase_17 AC 1,300 ms
84,244 KB
testcase_18 AC 138 ms
55,900 KB
testcase_19 AC 1,703 ms
100,116 KB
testcase_20 AC 128 ms
55,820 KB
testcase_21 AC 125 ms
55,812 KB
testcase_22 AC 476 ms
68,300 KB
testcase_23 AC 1,764 ms
99,748 KB
testcase_24 AC 1,760 ms
99,948 KB
testcase_25 AC 1,386 ms
91,608 KB
testcase_26 AC 124 ms
56,184 KB
testcase_27 AC 138 ms
58,180 KB
testcase_28 AC 600 ms
67,752 KB
testcase_29 AC 206 ms
62,964 KB
testcase_30 AC 125 ms
56,084 KB
testcase_31 AC 127 ms
55,476 KB
testcase_32 AC 193 ms
58,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	
	static int N;
	static int ans = Integer.MAX_VALUE;
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		scan.close();
		boolean flag[] = new boolean[N+1];
		
		Queue<Node> q = new LinkedList<>();
		q.add(new Node(1,1));
		
		while(!q.isEmpty()) {
			Node v = q.poll();
			
			if(v.number == N) {
				ans = Math.min(ans,  v.steps);
				continue;
			}
			
			flag[v.number] = true;
			
			int nextRight = v.number + (Integer.bitCount(v.number));
			if(nextRight <= N && !flag[nextRight]) { 
				q.add(new Node(nextRight, v.steps+1));
			}
			
			int nextLeft = v.number - (Integer.bitCount(v.number));
			if(nextLeft >= 1 && !flag[nextLeft]) { 
				q.add(new Node(nextLeft, v.steps+1));
			}
		}
		
		if(ans == Integer.MAX_VALUE) {
			System.out.println("-1");
		} else { 
			System.out.println(ans);
		}
	}
}

class Node {
	int number;
	int steps;
	Node(int number , int steps) {
		this.number = number;
		this.steps = steps;
	}
}
0