import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static int n;
static int[] distance;
static boolean[] judge;
public static void main(String[] args) throws IOException{
n = Integer.parseInt(br.readLine());
distance = new int[n+1];
judge = new boolean[n+1];
Arrays.fill(distance , 0x3f3f3f3f);
distance[1] = 0;
BFS();
out.write(distance[n] + "");
out.flush();
out.close();
br.close();
}
private static void BFS(){
Point start = new Point();
start.x = 1;
start.cost = 0;
Deque<Point> deque = new LinkedList<>();
deque.offer(start);
while(!deque.isEmpty()){
Point s = deque.poll();
if(s.x - 1 >= 1 && !judge[s.x-1]){
judge[s.x-1] = true;
Point temp = new Point();
temp.x = s.x - 1;
temp.cost = s.cost + 1;
distance[s.x-1] = temp.cost;
deque.offer(temp);
}
if(s.x + 1 <= n && !judge[s.x+1]){
judge[s.x+1] = true;
Point temp = new Point();
temp.x = s.x + 1;
temp.cost = s.cost + 1;
distance[s.x+1] = temp.cost;
deque.offer(temp);
}
if(s.x * 2 <= n && !judge[s.x*2]){
judge[s.x*2] = true;
Point temp = new Point();
temp.x = s.x * 2;
temp.cost = s.cost + 1;
distance[s.x*2] = temp.cost;
deque.offer(temp);
}
}
}
}
class Point{
int x;
int cost;
}
洛谷 B3626 跳跃机器人 Java
最新推荐文章于 2024-01-30 21:25:56 发布