Java 如何遍历布尔递归数组

Java 如何遍历布尔递归数组,java,arrays,recursion,boolean,Java,Arrays,Recursion,Boolean,练习: 构建一个递归(没有循环),你进入的每个单元格都是你可以走的步数,它可以是右/左,直到你到达最后一个单元格。如果无法到达最后一个单元格,则返回false,否则返回true。 必须从索引0开始 我的问题:我构建了程序,它不工作,我能够到达最后一个单元格,但在输出中我得到了错误,我理解为什么我得到错误,但我不知道如何修复它 测试: public static void main(String[] args) { // Q - isWay System.out.println("

练习: 构建一个递归(没有循环),你进入的每个单元格都是你可以走的步数,它可以是右/左,直到你到达最后一个单元格。如果无法到达最后一个单元格,则返回false,否则返回true。 必须从索引0开始

我的问题:我构建了程序,它不工作,我能够到达最后一个单元格,但在输出中我得到了错误,我理解为什么我得到错误,但我不知道如何修复它

测试:

public static void main(String[] args)
{
// Q - isWay
        System.out.println("\nTesting Question 3\n==================");
        int[] a1 = {2,4,1,6,4,2,4,3,5};
        System.out.println("a = {2,4,1,6,4,2,4,3,5}");
        System.out.println("Ex14.isWay(a) is: " + Ex14.isWay(a1)); //need to return true
        int[] a2 = {1,4,3,1,2,4,3};
        System.out.println("a2 = {1,4,3,1,2,4,3}");
        System.out.println("Ex14.isWay(a2) is: " + Ex14.isWay(a2));//need to return false
}


public class Ex14
{
    public static boolean isWay(int[] a)
        {
            int i = 0;
            if(a.length <= 1)
                return false;
            return isWay(a , 0);
        }

        public static boolean isWay(int[] a,int i)
        {     
            int temp1 , temp2;
            if(i == a.length-1)
                return true;
            if(!((a[i]+i < a.length) && (i-a[i] >= 0))) // can't go right and left
                return false;
            else if(a[i] > 0)
            {
                if(a[i]+i < a.length) // go right
                {
                    temp1 = a[i] + i;
                    a[i] = -1;
                    return isWay(a, temp1);
                }
                else if (i-a[i] >= 0) // go left
                {
                    temp2 = i - a[i];
                    a[i] = -1;
                    return isWay(a, temp2);   
                }    
            }
            return false;
        }
}
publicstaticvoidmain(字符串[]args)
{
//Q-isWay
System.out.println(“\n测试问题3\n=========================”;
int[]a1={2,4,1,6,4,2,4,3,5};
System.out.println(“a={2,4,1,6,4,2,4,3,5}”);
System.out.println(“Ex14.isWay(a)是:”+Ex14.isWay(a1));//需要返回true
int[]a2={1,4,3,1,2,4,3};
System.out.println(“a2={1,4,3,1,2,4,3}”);
System.out.println(“Ex14.isWay(a2)是:”+Ex14.isWay(a2));//需要返回false
}
公共类Ex14
{
公共静态布尔isWay(int[]a)
{
int i=0;
if(a.length=0))//不能左右移动
返回false;
else如果(a[i]>0)
{
如果(a[i]+i=0)//向左走
{
temp2=i-a[i];
a[i]=-1;
返回isWay(a,temp2);
}    
}
返回false;
}
}

我没有得到您想要做的事情,但下面是一个递归函数的示例,该函数对数组中的所有布尔函数都有效

public static boolean recurse(boolean[] ary)
{
    if (ary.length == 1) {
        return ary[0];
    }

    return ary[0] && recurse(Arrays.copyOfRange(ary, 1, ary.length));
}
测试:

public static void main(String[] args)
{
    boolean[] ary = { true, true, true, true, true};

    System.out.println(recurse(ary));

    boolean[] ary2 = { true, true, false, true, true};

    System.out.println(recurse(ary2)); 
}
  • 真的
  • 假的

我希望它能帮助您解决问题,否则您可以进一步解释您希望如何使用此函数

返回
false的条件是错误的

if(!((a[i]+i < a.length) && (i-a[i] >= 0)))
其思想是,如果您可以同时向左和向右走,并且向右走会导致死胡同,那么当向右走的递归调用返回时,您必须尝试向左走


对于
{1,4,3,6,1,2,4,3}
{2,4,1,6,4,2,4,3,5}

这就是我如何解决它的:

public static void main(String[] args)
{
    int[] a = { 2, 4, 1, 6, 4, 2, 4, 3, 5 };
    System.out.println(isWay(a)); // true

    int[] a1 = { 1, 4, 3, 1, 2, 4, 3 };
    System.out.println(isWay(a1)); // false
}

public static boolean isWay(int[] a)
{
    return isWay(a, 0);
}

private static boolean isWay(int[] a, int i)
{
    if (i == a.length - 1) // if we are in the last index
        return true;

    if (i >= a.length || i < 0) // boundaries
        return false;
    
    if (a[i] == -1) // if marked return false to prevent infinite recursion..
        return false;

    int temp = a[i]; // mark where we already have been...
    a[i] = -1;

    boolean r1 = isWay(a, i + temp); // we are trying to move right
    boolean r2 = isWay(a, i - temp); // we are trying to move left

    a[i] = temp; // revert changes

    return r1 || r2;
}
publicstaticvoidmain(字符串[]args)
{
int[]a={2,4,1,6,4,2,4,3,5};
System.out.println(isWay(a));//true
int[]a1={1,4,3,1,2,4,3};
System.out.println(isWay(a1));//false
}
公共静态布尔isWay(int[]a)
{
返回isWay(a,0);
}
私有静态布尔isWay(int[]a,int i)
{
if(i==a.length-1)//如果我们在最后一个索引中
返回true;
如果(i>=a.length | i<0)//边界
返回false;
if(a[i]==-1)//if标记返回false以防止无限递归。。
返回false;
int temp=a[i];//标记我们已经到达的位置。。。
a[i]=-1;
布尔r1=isWay(a,i+temp);//我们正试图向右移动
布尔r2=isWay(a,i-temp);//我们正试图向左移动
a[i]=temp;//还原更改
返回r1 | | r2;
}

好的。。。我不太清楚你在问什么。你能更清楚一点吗?当然,我是递归新手,现在我不明白我在递归中做得不好的地方,我总是错的,我希望你能告诉我错误,解释我做错了什么。在我看来,你的递归很有效,只是你实现的逻辑有缺陷谢谢你的回应。如果我不能左右移动,这不是问题,所以我被卡住了,我需要返回false,否则我继续problem@asaf是的,但是如果你可以向右走,但不能向左走,你应该向右走,而不是返回false。我试过你说的,现在仍然有效。2.这个条件只有在我被卡住的时候才会出现,所以我需要返回false,否则我会保留going@asaf我用
{1,4,3,6,1,2,4,3}
数组尝试了固定代码。它返回true,正如它应该返回的那样(0->1->5->7)。现在试试这个,它还需要返回true{2,4,1,6,4,2,4,3,5}
public static boolean isWay(int[] a,int i) {
    int temp1 , temp2;
    if(i == a.length-1) {
        return true;
    }
    boolean found = false;
    if(a[i]+i < a.length && a[a[i]+i] > 0) { // go right
        temp1 = a[i] + i;
        a[i] = -1;
        found = isWay(a, temp1);
        if (!found) {
            a[i] = temp1 - i; // must restore a[i] to its original value, in order 
                              // to be able to go left
        }
    }
    if (!found && i-a[i] >= 0 && a[i - a[i]] > 0) { // go left
        temp2 = i - a[i];
        a[i] = -1;
        found = isWay(a, temp2);   
    }
    return found;
}
public static void main(String[] args)
{
    int[] a = { 2, 4, 1, 6, 4, 2, 4, 3, 5 };
    System.out.println(isWay(a)); // true

    int[] a1 = { 1, 4, 3, 1, 2, 4, 3 };
    System.out.println(isWay(a1)); // false
}

public static boolean isWay(int[] a)
{
    return isWay(a, 0);
}

private static boolean isWay(int[] a, int i)
{
    if (i == a.length - 1) // if we are in the last index
        return true;

    if (i >= a.length || i < 0) // boundaries
        return false;
    
    if (a[i] == -1) // if marked return false to prevent infinite recursion..
        return false;

    int temp = a[i]; // mark where we already have been...
    a[i] = -1;

    boolean r1 = isWay(a, i + temp); // we are trying to move right
    boolean r2 = isWay(a, i - temp); // we are trying to move left

    a[i] = temp; // revert changes

    return r1 || r2;
}