JAVA蓝桥杯之BFS

在这里插入图片描述

import java.util.LinkedList;
import java.util.Scanner;
public class Main {
	static int k,ans=10000;
	static int count[];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int  n = sc.nextInt();
		k = sc.nextInt();
		dfs(n,0);
		System.out.println(ans);
		sc.close();
	}
	//dfs
	private static void dfs(int n, int time) {
		if(n==k) {
			if(time < ans)
				ans = time;
			return ;
		}
		if(time > ans) {  //如果时间大于之前的答案 则不需要
			return ;
		}
		if(n > 2 * k) {
			return;
		}
		if(n>k) {	
			dfs(n-1,time+1);
		}
		dfs(n+1,time+1);
		dfs(n-1,time+1);
		dfs(n*2,time+1);
	}
	//bfs
	static boolean[] flag =new boolean[10000];
	
	private static void dfs2(int n, int t) {
		 Quene q = new  Quene();
		 q.in(new Step(n,0));
		 for(int i=0;i<flag.length;i++) {
			 flag[i] = true;
		 }
		 flag[n] = false;
		 while(!q.isEmpty())
		 {
			 Step s =q.getFrist();
			 if(s.x == k)
			 {
				 System.out.println(s.times);
			 }
			 else
			 {
				 //三种移动状态,符合条件加入队列中
		            if(s.x-1>=0&&!flag[s.x-1]) 
		            {
		                q.in(new Step(s.x-1,s.times+1));
		                flag[s.x-1]=false;
		            }
		            if(s.x+1>=0&&!flag[s.x+1])
		            {
		                q.in(new Step(s.x+1,s.times+1));
		                flag[s.x+1]=false;
		            }
		            if(s.x*2>=0&&!flag[s.x*2])
		            {
		                q.in(new Step(s.x*2,s.times+1));
		                flag[s.x*2]=false;
		            }
		            q.deleteFrist();
			 }
			 
		 }
	}
}
class Quene{
	
	LinkedList<Step> link = new LinkedList<Step>();
	
	//模拟 入队
	public  void in(Step s) {
		link.addFirst(s);
	}
	//模拟 出队
	public Object out() {
		return link.removeLast();
	}
	//模拟 队列结构是否为空
	public boolean isEmpty(){
		return link.isEmpty();
	}
	//模拟 队列结构得到第一个元素
	public Step getFrist() {
		return link.getFirst();
	}
	//模拟 队列结构去除第一个元素
	public Step deleteFrist() {
		return link.removeFirst() ;
	}
	
}
class Step
{
    int x; //记录当前位置
    int times; //记录时间
	public Step(int x, int times) {
		this.x = x;
		this.times = times;
	}
	public Step() {

	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑瞳丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值