結果

問題 No.3 ビットすごろく
ユーザー jp_stejp_ste
提出日時 2016-02-12 04:13:03
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,070 bytes
コンパイル時間 3,176 ms
コンパイル使用メモリ 77,652 KB
実行使用メモリ 101,420 KB
最終ジャッジ日時 2023-10-22 02:27:51
合計ジャッジ時間 16,722 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 115 ms
57,504 KB
testcase_03 AC 133 ms
59,504 KB
testcase_04 WA -
testcase_05 AC 165 ms
59,992 KB
testcase_06 AC 126 ms
59,580 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 146 ms
59,992 KB
testcase_13 AC 112 ms
56,180 KB
testcase_14 AC 391 ms
70,508 KB
testcase_15 AC 1,162 ms
92,308 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 115 ms
57,400 KB
testcase_22 AC 393 ms
70,896 KB
testcase_23 AC 1,420 ms
98,792 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 117 ms
57,120 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.LinkedList;
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();
		boolean flag[] = new boolean[N+1];
		int ans = Integer.MAX_VALUE;
			
		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.number);
				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