简体   繁体   English

查找所有可能的路径算法

[英]Find All Possible Paths Algorithm

Hello there StackOverFlow! 您好,StackOverFlow! I am posting here today because I have a problem here in Java where I am trying to compute the all the possible combinations of pogo sticks that my character may use to move. 我今天在这里发布消息是因为我在Java中遇到了一个问题,我试图计算角色可能用来移动的弹簧娃娃的所有可能组合。 The character uses pogo sticks which all have a distance, given by user input. 角色使用按用户输入给定距离的弹簧娃娃。

Likewise, the total distance is also given via user input and all possible paths are to be found. 同样,总距离也可以通过用户输入给出,并且可以找到所有可能的路径。 I have shown my function below with the output and the desired output that I can't seem to get quite right. 我在下面用输出和所需的输出显示了我的功能,但我似乎不太正确。

I have been stuck on this problem for a while and I am really hoping somebody can help me out here! 我已经在这个问题上停留了一段时间,我真的希望有人可以在这里帮助我!

/*
 * First integer in input
 */
int totalDistance;

/*
 * The remaining integers in the input
 */
ArrayList<Integer> pogoSticks = new ArrayList<Integer>();

private void findPaths() {
        ArrayList<ArrayList<Integer>> possibleSticks = new ArrayList<ArrayList<Integer>>();

        for (int i = 0; i < pogoSticks.size(); i++) {

            int pogoStickDistance = pogoSticks.get(i);

            if (pogoStickDistance == totalDistance) {
                if (!possibleSticks.contains(new ArrayList<Integer>(pogoStickDistance))) {
                    ArrayList<Integer> list = new ArrayList<Integer>();
                    list.add(pogoStickDistance);
                    possibleSticks.add(list);
                }
            } else if (pogoStickDistance < totalDistance) {
                int remainingDistance = totalDistance;
                ArrayList<Integer> possibleSubSticks = new ArrayList<Integer>();

                possibleSubSticks.add(pogoStickDistance);
                remainingDistance -= pogoStickDistance;

                for (int j = 0; j < pogoSticks.size(); j++) {

                    int pogoStickDistance1 = pogoSticks.get(j);
                    if (pogoStickDistance1 == remainingDistance) {
                        possibleSubSticks.add(pogoStickDistance1);

                        possibleSticks.add(possibleSubSticks);
                        break;
                    } else if (pogoStickDistance1 < remainingDistance) {
                        possibleSubSticks.add(pogoStickDistance1);
                        remainingDistance -= pogoStickDistance1;
                    }

                    if (j == (pogoSticks.size() - 1) && pogoStickDistance1 != remainingDistance) {
                        j = 0;
                    }
                }
            }

        }

        System.out.println(possibleSticks);
    }

Here is the output that I get from running the function above: 这是我从运行上面的函数得到的输出:

Enter input: 5 10 4 1 2
[[4,1], [1,4], [2,1,2]]

Note that 5 is the distance, and 10 , 4 , 1 , and 2 are the distances that a pogo stick may travel. 注意, 5是距离,和1041 ,和2是一个弹簧单高跷可以行进的距离。

The issue is that these are not all the possible paths! 问题是这些并不是所有可能的路径! For example, it is missing the paths such as [1, 1, 1, 1, 1] or [2, 2, 1] . 例如,它缺少诸如[1, 1, 1, 1, 1] 1,1,1,1,1 [1, 1, 1, 1, 1][2, 2, 1] [1, 1, 1, 1, 1]之类的路径。

Can anybody please help me modify my function to include these? 有人可以帮我修改我的功能以包括这些吗? I believe it is happening because once my loop finds the first occurrence of a pogo stick distance that's less than the remaining distance it will immediately use that path and ignore other possibilities. 我相信这种情况正在发生,因为一旦我的循环发现第一次出现的弹簧娃娃距离小于剩余距离,它将立即使用该路径而忽略其他可能性。

                for(int i = 0;i < pogoSticks.size();i++){
                   //part to calculate small enough 
        int[] temps = new int[pogoSticks.size];
        int temp1 = 0; 
                   for(int j; j< pogoStricks.size();i++){
                   if(pogoSticks.getIndex(j) + k <= totalDisatnce){
        temps[temp1] = pogoSticks.getIndex(j);
        }
    //code to calculate number of paths to get to TotalDistance

This should do half the job, now you just need a method to calculate the distance from all the temps variables. 这应该完成一半的工作,现在您只需要一种方法即可计算所有临时变量的距离。 I suggest you subtract each value from the TotalDistance and see which numbers added up would equal that. 我建议您从TotalDistance中减去每个值,然后看看相加的数字是否相等。

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

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