第十届蓝桥杯扫地机器人(递归回溯)

该博客文章介绍了一个程序,它通过深度优先搜索(DFS)算法来解决关于机器人在给定位置上的排列问题。程序接收输入参数,包括机器人的数量和它们的位置,然后找出最小的最大距离。在过程中,程序会计算每个机器人的左右边界,并递归地更新最大距离,最终输出最小的最大距离。
摘要由CSDN通过智能技术生成
public class p10_7 {
	static int k;
	static int[] pos;
	static int n;
	static int count=Integer.MAX_VALUE;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		k = sc.nextInt();
		pos=new int[k+1];
		for (int i = 1; i <=k; i++) {
			pos[i]=sc.nextInt();
		}
		Arrays.sort(pos);
		dfs(1, 1,0);
		System.out.println(count);
	}
	// index代表第几胎机器人 ,left代表左边界
	public static void dfs(int index,int left,int curmax) {
		int d=0;
		if(index==k) {
			d=(n-left)*2;
			curmax=Math.max(curmax, d);
			count=Math.min(count, curmax);
			return;
		}
		// 确定右边界
		for (int i = pos[index]; i < pos[index+1]; i++) {
			d=(i-left)*2;
			curmax=Math.max(curmax, d);
			dfs(index+1, i+1,curmax);
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值