package com.od;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Demo20 {
/*
一个正整数数组 设为nums
最大为100个成员
求从第一个成员开始正好走到数组最后一个成员所使用的最小步骤数
3 5 9 4 2 6 8 3 5 4 3 9
要求:
1. 第一步 必须从第一元素起 且 1<=第一步步长<len/2 (len为数组长度)
2. 从第二步开始只能以所在成员的数字走相应的步数,不能多不能少,
如果目标不可达返回-1
只输出最小的步骤数量
3. 只能向数组的尾部走不能向回走
输入描述:
有正整数数组 空格分割
数组长度<100
输出描述 :
正整数 最小步数
不存在输出-1
例子:
输入
7 5 9 4 2 6 8 3 5 4 3 9
输出
2
第一个可选步长选择2
从第一个成员7开始走两步到9
第二步:从9经过9个成员到最后
例子:
输入
1 2 3 7 1 5 9 3 2 1
输出
-1
*/
public static List<Integer> list = new ArrayList<>();
public static int cnt = 1;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] s = in.nextLine().split(" ");
int[] n = new int[s.length];
for (int i = 0; i < s.length; i++) {
n[i] = Integer.parseInt(s[i]);
}
int len = n.length / 2;
// 7 5 9 4 2 6 8 3 5 4 3 9
for (int i = 1; i <= len; i++) {
cnt = 1;
int start = i;
// if (n[start] > len) continue;
int end = start + n[start] + 1;
deal(start, end, n);
}
for (Integer integer : list) {
System.out.println(integer + " ");
}
}
private static void deal(int start, int end, int[] n) {
if (end == n.length - 1) {
list.add(cnt);
return;
} else if (end < n.length - 1) {
start = end;
// 7 5 9 4 2 6 8 3 5 4 3 9
end = start + n[start] + 1;
cnt++;
deal(start, end, n);
} else {
//list.add(-1);
return;
}
}
}
使用递归例子---OD算法
最新推荐文章于 2024-08-07 08:23:12 发布