結果

問題 No.3 ビットすごろく
ユーザー Humming_sangoHumming_sango
提出日時 2023-03-03 18:40:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 93 ms / 5,000 ms
コード長 2,514 bytes
コンパイル時間 2,604 ms
コンパイル使用メモリ 78,920 KB
実行使用メモリ 54,728 KB
最終ジャッジ日時 2023-10-18 01:12:08
合計ジャッジ時間 6,150 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,588 KB
testcase_01 AC 57 ms
53,604 KB
testcase_02 AC 55 ms
53,596 KB
testcase_03 AC 65 ms
53,620 KB
testcase_04 AC 56 ms
52,508 KB
testcase_05 AC 84 ms
54,252 KB
testcase_06 AC 70 ms
53,648 KB
testcase_07 AC 59 ms
52,528 KB
testcase_08 AC 71 ms
54,172 KB
testcase_09 AC 89 ms
54,176 KB
testcase_10 AC 83 ms
54,204 KB
testcase_11 AC 86 ms
54,180 KB
testcase_12 AC 72 ms
54,260 KB
testcase_13 AC 60 ms
53,628 KB
testcase_14 AC 87 ms
54,724 KB
testcase_15 AC 91 ms
54,172 KB
testcase_16 AC 80 ms
54,196 KB
testcase_17 AC 81 ms
54,176 KB
testcase_18 AC 59 ms
52,556 KB
testcase_19 AC 93 ms
54,164 KB
testcase_20 AC 56 ms
53,612 KB
testcase_21 AC 55 ms
52,504 KB
testcase_22 AC 88 ms
54,172 KB
testcase_23 AC 92 ms
54,172 KB
testcase_24 AC 90 ms
54,204 KB
testcase_25 AC 88 ms
54,180 KB
testcase_26 AC 54 ms
53,592 KB
testcase_27 AC 60 ms
53,624 KB
testcase_28 AC 87 ms
54,728 KB
testcase_29 AC 89 ms
54,184 KB
testcase_30 AC 55 ms
53,600 KB
testcase_31 AC 57 ms
53,616 KB
testcase_32 AC 86 ms
54,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Queue;
import java.util.StringTokenizer;
 
public class Main {
	static ArrayList<ArrayList<Integer>> graph_list = new ArrayList<ArrayList<Integer>>();
	
	public static void main(String[] args) {
		FastReader fr = new FastReader();
		int n = fr.nextInt();
		for(int i=0;i<=n;i++) {
			graph_list.add(new ArrayList<Integer>());
		}
		for(int i=1;i<=n;i++) {
			int bit = popcount(i);
			if(i-bit>=1) graph_list.get(i).add(i-bit);
			if(i+bit<=n) graph_list.get(i).add(i+bit);
		}
		int dist = bfs_dist(n,1,n);
		if(dist==Integer.MAX_VALUE) {
			dist=-1;
		}else {
			dist++;
		}
		System.out.println(dist);
	}
	
	static int bfs_dist(int n, int root, int goal) {
		Queue<Integer> todo = new ArrayDeque<Integer>();
		int[] dist = new int[n+1];
		Arrays.fill(dist, Integer.MAX_VALUE);
		dist[root] = 0;
		todo.add(root);
		while(!todo.isEmpty()) {
			Integer node = todo.poll();
			for(Integer i:graph_list.get(node)) {
				if(dist[i]>dist[node]+1) {
					todo.add(i);
					dist[i]=dist[node]+1;
				}
			}
		}
		return dist[goal];
	}
	
	static int popcount(int x) {
		x = (x & 0x55555555) + (x >> 1 & 0x55555555);
		x = (x & 0x33333333) + (x >> 2 & 0x33333333);
		x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
		x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff);
		return (x & 0x0000ffff) + (x >>16 & 0x0000ffff);
	}
}

class FastReader {
	private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000);
	private StringTokenizer tokenizer;
	
	String next() {
		try{
			while(tokenizer==null || !tokenizer.hasMoreTokens()) {
				tokenizer = new StringTokenizer(reader.readLine());
			} 
		}catch(IOException e){
			e.printStackTrace();
		}
		return tokenizer.nextToken();
		
	}
	
	int nextInt() {
		return Integer.parseInt(next());
	}
	
	long nextLong() {
		return Long.parseLong(next());
	}
	
	double nextDouble(){
		return Double.parseDouble(next());
	}
	
	char[] nextCharArray() {
        return next().toCharArray();
    }
	
	char nextSingleChar() { //一文字じゃなかったら' '(空白文字)返す
		char[] c = next().toCharArray();
		if(c.length==1) {
			return c[0]; 
		}else {
			return ' ';
		}
	}
	
	String[] nextStringArray(int n) {
	        String[] s = new String[n];
	        for (int i = 0; i < n; i++) {
	            s[i] = next();
	        }
	        return s;
	    }

}
0