简体   繁体   English

Java Puzzle在整数数组中左右移动

[英]Java Puzzle moving left and right in an array of integers

I have an array of integers. 我有一个整数数组。 Starting from the first position, I then add or subtract the value at the given index to move around in the array. 从第一个位置开始,然后添加或减去给定索引处的值以在数组中移动。 The purpose of the puzzle is to get to the last element of the array which is 0. I'm aware that this problem is already solved here with recursion, but I'm supposed to give a non-recursive solution. 难题的目的是到达数组的最后一个元素0。我知道这里已经通过递归解决了这个问题,但是我应该给出一个非递归的解决方案。 To avoid an infinite loop, I make a condition 为了避免无限循环,我做了一个条件

if (a[index] == a[index+temp]) 

and the code works fine when I pass an array like this: 当我传递这样的数组时,代码工作正常:

int a [] = { 3, 1, 2, 3, 0 };

But then I pass int a [] = {3, 6, 4, 3, 3, 4, 3, 5, 3, 0 } and it tells me that the puzzle does not have a solution, which is not true. 但是然后我将int a [] = {3, 6, 4, 3, 3, 4, 3, 5, 3, 0 }传递给int a [] = {3, 6, 4, 3, 3, 4, 3, 5, 3, 0 } ,它告诉我这个难题没有解决方案,这是不正确的。 Here is part of my code: 这是我的代码的一部分:

       int temp;
       int index = 0;

       while (index < (a.length-1) && index >= 0)
            {
                temp = a[index];

                if ((index+temp) <= a.length-1 )
                {
                    if (a[index] == a[index+temp]){
                        solution = false;
                        break;
                    }
                    index = index + temp;
                }
                else if ((index-temp) >=0)
                {

                    index = index - temp;
                }

        }   

I attach a photo from my assignment which explains the behavior of the algorithm. 我附上一张作业中的照片,解释了算法的行为。 在此处输入图片说明

What you have here is basically a directed unweighted graph. 您基本上拥有的是有向无权图。 Each index is connected with 1 or 2 other indexes. 每个索引都与1个或2个其他索引相连。

Now, having that in mind, you can solve this problem easily with a " breadth first search " algorithm which works pretty well without recursion. 现在,考虑到这一点,您可以使用“ 广度优先搜索 ”算法轻松解决此问题,该算法无需递归即可很好地工作。

Here is pretty verbose implementation example: https://ideone.com/yzeBzz 这是非常详细的实现示例: https : //ideone.com/yzeBzz

List<Integer> solve(int... a) {
    //Value in each element is the index, from where we can come here
    int[] path = new int[a.length];
    Arrays.fill(path, -1); //No index is accessible yet

    //Queue of positions that were visited from somewhere, but nothing was tried to be 
    //visited from them. At the beginning, 0 is in the list, because it's starting point.
    //Then, if we visit index 3, it is added to this list for later processing.
    Queue<Integer> posQueue = new LinkedList<>();
    posQueue.add(0);
    path[0] = 0; //0 index is accessible from itself, this is starting position

    while (!posQueue.isEmpty()) {
        int pos = posQueue.remove();
        int prPos = pos - a[pos];
        int nxPos = pos + a[pos];
        if (prPos >= 0 && path[prPos] == -1) {
            path[prPos] = pos;
            posQueue.add(prPos);
        }
        if (nxPos < a.length && path[nxPos] == -1) {
            path[nxPos] = pos;
            posQueue.add(nxPos);
        }

        if (path[a.length-1] != -1) {
            break;
        }
    }

    if (path[a.length-1] == -1) {
        return null;
    }

    //Collect the path
    List<Integer> result = new ArrayList<>();
    int idx = a.length-1;
    while (idx != 0) {
        result.add(0, idx);
        idx = path[idx];
    }
    result.add(0, 0);
    return result;
}

As with any breadth search algorithm, the complexity is O(N). 与任何广度搜索算法一样,复杂度为O(N)。

This might work: 这可能起作用:

boolean solve(int... a) {
    for (int i = 0; i < 1 << a.length; i++) {
        int pos = 0;
        for (int j = 0; j < 32; j++) {
            if ((i & (1 << j)) == 0) {
                if ((pos -= a[pos]) < 0) {
                    break;
                }
            } else {
                if ((pos += a[pos]) >= a.length) {
                    break;
                }
            }
            if (a[pos] == 0) {
                return true;
            }
        }
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM