问题如下:
你有一个整数数组,例如[2,3,1,4,0]。您想要找到最后一个索引的路径。您可以从索引0开始向左或向右移动。您可以沿阵列移动的步数取决于索引位置的值。最后,您要打印所有路径。
在我的例子中,你从索引0开始,它包含2.你不能向左移动,因此你向右移动。现在你在索引2,它包含1.然后你可以向左移动1到达包含3的索引1,然后向右移动3到达最后一个索引。
我在实施方面遇到了问题,并且知道如何打印出所有可能的解决方案。
我的代码如下:
public static boolean solveH(int index){
if(index == data.size()-1){
printSol(stages);
return true;
}
boolean success = false;
if(!success && (index + Integer.parseInt(data.get(index)) < data.size())){
String temp = data.get(index);
data.set(index, temp + "R");
stages.add(copy(data));
data.set(index, temp);
success = solveH(index + Integer.parseInt(data.get(index)));
stages.remove(stages.size()-1);
}
if(!success && (index - Integer.parseInt(data.get(index)) > 0)){
String temp = data.get(index);
data.set(index, temp + "L");
stages.add(copy(data));
data.set(index, temp);
success = solveH(index - Integer.parseInt(data.get(index)));
stages.remove(stages.size()-1);
}
return false;
}
答案 0 :(得分:0)
我的方法是:在每次现场访问时,访问权限(事实上 - 每一步都是分支)。
好的 - 首先,就像你一样:看看我们是否达到目标。
然后,保存该索引处的值以左右踩踏,然后将值设置为0作为“已访问”的标记。你无论如何都冻结在零场上,并且到达目标的检查是基于index = last index。
而不是字符串和解析,只需将普通数字存储在数组中。
代码:
public class StepArray {
// using static fields/methods is a bit ugly. Maybe improve main,
// to allow passing an array to visit dynamically.
static int[] data = new int [] {2, 3, 1, 4, 0};
static int len = data.length;
public static boolean solveH (int index){
if (index == len - 1) {
System.out.println ("found at index " + index + "!");
return true;
}
// save the value before overwriting:
int step = data[index];
// already visited?
if (step == 0) return false;
// mark for later visitors as visited by setting to 0:
data[index] = 0;
if (index + step < len) {
System.out.print (" R:" + step);
solveH (index + step);
}
// no need to visit 0 again, which is always the starting point
if (index - step > 0) {
System.out.print (" L:" + step);
solveH (index - step);
}
return false;
}
public static void main (String args[]) {
solveH (0);
}
}
以下是改进版本以获得更多乐趣和灵活性(核心算法仅进行了外观修改):
import java.util.Random;
public class ArraySteps {
int[] data;
int len;
public ArraySteps (int [] values) {
data = values;
len = data.length;
solveH (0);
System.out.println ();
}
boolean solveH (int index) {
if (index == len - 1) {
System.out.println (" found at index " + index + "!");
return true;
}
int step = data[index];
// already visited
if (step == 0) return false;
// mark for later visitors as visited by setting to 0:
data[index] = 0;
boolean r = false, l = false;
if (index + step < len) {
System.out.print (" R:" + step);
r = solveH (index + step);
}
if (index - step > 0) {
System.out.print (" L:" + step);
l = solveH (index - step);
}
if (r || l) return true;
System.out.println (" dead end.");
return false;
}
public static void main (String args[]) {
// default, no args:
int [] param;
System.out.println ("args.length: " + args.length);
if (args.length == 0)
{
System.out.println ("default: ");
param = new int [] {2, 3, 1, 4, 0};
}
// one arg: Generate count N random values
else if (args.length == 1)
{
System.out.println ("Random: ");
int anz = Integer.parseInt (args[0]);
Random r = new Random ();
param = new int [anz];
for (int i = 0; i < anz - 1; ++i) {
param [i] = r.nextInt (Math.min (anz/2, 9)) + 1;
}
param[anz-1] = 0;
}
else {
System.out.println ("User defined: ");
param = new int [args.length + 1];
for (int i = 0; i < args.length; ++i)
{
param[i] = Integer.parseInt (args[i]);
}
param [args.length] = 0;
}
show (param);
new ArraySteps (param);
}
private static void show (int[] ls)
{
for (int i: ls)
System.out.print (i + " ");
System.out.println ();
}
}