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);
}
}
}
第十届蓝桥杯扫地机器人(递归回溯)
最新推荐文章于 2023-05-04 11:44:52 发布
该博客文章介绍了一个程序,它通过深度优先搜索(DFS)算法来解决关于机器人在给定位置上的排列问题。程序接收输入参数,包括机器人的数量和它们的位置,然后找出最小的最大距离。在过程中,程序会计算每个机器人的左右边界,并递归地更新最大距离,最终输出最小的最大距离。
摘要由CSDN通过智能技术生成