【问题标题】:how to traverse a Boolean recursion array如何遍历布尔递归数组
【发布时间】:2018-01-09 11:05:01
【问题描述】:

练习: 建立一个递归(没有循环),你进入的每个单元格都是你可以走的步数,它可以是右/左,直到你到达最后一个单元格。如果您无法到达最后一个单元格,则返回 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;
        }
}

【问题讨论】:

  • 好的......我不确定你在问什么。你能说得清楚一点吗?
  • 当然,我是递归新手,现在我不明白我在递归中做得不好的地方总是错误的,我希望你能告诉我错误并解释我的错误我做错了。
  • 在我看来你的递归有效,只是你实现的逻辑有缺陷

标签: java arrays recursion boolean


【解决方案1】:

您返回false的条件错误。

if(!((a[i]+i < a.length) && (i-a[i] >= 0)))

应该是

if(!((a[i]+i < a.length) || (i-a[i] >= 0)))

如果您无法向左向右继续,您想返回false。您的条件测试您是否不能同时向左向右进行。

编辑:

我最初建议的修复方法还不够,因为如果您遇到死胡同,您的方法必须能够回溯。

这是另一种方法:

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;
}

想法是,如果你可以左右走,并且向右走会导致死胡同,你必须在向右的递归调用返回时尝试向左走。

这对于 {1,4,3,6,1,2,4,3}{2,4,1,6,4,2,4,3,5} 都返回 true。

【讨论】:

  • 感谢您的回复。如果我不能左右移动,这不是问题,所以我被卡住了,我需要返回 false 否则我会继续处理问题
  • @asaf 是的,但是如果你可以向右但不能向左,你应该向右而不是返回false。跨度>
  • 1.我试过你说的,它现在仍然有效 2.这种情况只有当我被卡住所以我需要返回 false 否则我继续前进
  • @asaf 我用{1,4,3,6,1,2,4,3} 数组尝试了固定代码。它返回 true,因为它应该 (0->1->5->7)。
  • 现在试试这个,它也需要返回true {2,4,1,6,4,2,4,3,5}
【解决方案2】:

我没有得到你想要做的,但这里是一个递归函数的例子,它有效 && 数组中的所有布尔值

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)); 
}
  • 是的
  • 错误

我希望能帮助你解决你的问题,否则你可以解释更多你想用这个功能做什么

【讨论】:

    【解决方案3】:

    我就是这样解决的:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2021-01-18
      • 1970-01-01
      相关资源
      最近更新 更多