简体   繁体   English

谁能帮助我理解这段代码?

[英]Can anyone help me understand this piece of code?

Can anyone please help me understand this code? 谁能帮我理解这段代码? This code is used to solve this problem. 此代码用于解决此问题。 You are playing a game on your cellphone. 您正在用手机玩游戏。 You are given an array of length n, indexed from 0 to n−1. 您会得到一个长度为n的数组,索引从0到n-1。 Each element of the array is either 0 or 1. You can only move to an index which contains 0. At first you are at the 0th position. 数组的每个元素都是0或1。您只能移动到包含0的索引。首先,您位于第0个位置。 In each move you can do one of the following things: 在每一步中,您都可以执行以下操作之一:

Walk one step forward or backward. 向前或向后走一步。 Make a jump of exactly length m forward. 向前精确跳一段长度m。 That means you can move from position x to x+1, x−1 or x+m in one move. 这意味着您可以从位置x移至x + 1,x-1或x + m。 The new position must contain 0. Also you can move to any position greater than n-1. 新位置必须包含0。您还可以移动到大于n-1的任何位置。

You can't move backward from position 0. If you move to any position greater than n−1, you win the game. 您不能从位置0向后移动。如果您移动到大于n-1的任何位置,您都将赢得比赛。

Given the array and the length of the jump, you need to determine if it's possible to win the game or not. 给定阵列和跳跃的长度,您需要确定是否有可能赢得比赛。

        n = sc.nextInt();
        m = sc.nextInt();
        field = new int[n];
        for (int i = 0; i < n; i++) {
            field[i] = sc.nextInt();
        }

        if (makeMove(0, new LinkedList<Integer>()))
            System.out.println("YES");
        else
            System.out.println("NO");

. . .

private boolean makeMove(int position, List<Integer> prevPositions) 
  {
  if (prevPositions.contains(position)) 
    return false;
    prevPositions.add(position);

    if (position < 0) return false;
    else if (position >= n) return true;
    else if (field[position] == 1) return false;
    else {
        return makeMove(position + m, prevPositions) ||
                makeMove(position + 1, prevPositions) ||
                makeMove(position - 1, prevPositions);
    }
}

input: 6 2 输入:6 2

   0 0 0 1 0 0

Output: Yes 输出:是

input: 6 2 输入:6 2

   0 0 1 1 0 0

Output: No 输出:否

So, I am assuming that you understand the concept of recursion, which is calling a method within itself, if not you may want to look it up. 因此,我假设您了解递归的概念,该递归本身正在内部调用一个方法,如果没有,则可能需要查找它。

The first section of code is very simple. 代码的第一部分非常简单。 It initializes the move length m and the array of length n and populates it with random binary digits. 它初始化移动长度m和长度n的数组,并用随机二进制数字填充它。

The makeMove method goes through a few base cases to see if a branch of recursion has failed or succeeded. makeMove方法会通过一些基本情况来查看递归分支是否失败或成功。

1.) if (prevPositions.contains(position)) return false; prevPositions.add(position); 1.) if (prevPositions.contains(position)) return false; prevPositions.add(position); if (prevPositions.contains(position)) return false; prevPositions.add(position);

After making a move, this code checks whether you have already gotten to this position. 移动后,此代码检查您是否已到达该位置。 If you have, it returns false because this case has already been known to be false, otherwise the method would have already returned true . 如果有,它将返回false因为已知这种情况下为false,否则该方法将已经返回true

2.) if (position < 0) return false; else if (position >= n) return true; else if (field[position] == 1) return false; 2.) if (position < 0) return false; else if (position >= n) return true; else if (field[position] == 1) return false; if (position < 0) return false; else if (position >= n) return true; else if (field[position] == 1) return false;

-You can't have a negative position, so that returns false . -您不能拥有负数,因此返回false -If your position is greater than n then you win, so return true -You can't move to a position that contains a non-zero digit, so return false -如果您的位置是大于n那么你就赢了,所以回到true -你不能移动到包含非零位的位置,所以返回false

3.) return makeMove(position + m, prevPositions) || makeMove(position + 1, prevPositions) || makeMove(position - 1, prevPositions); 3.) return makeMove(position + m, prevPositions) || makeMove(position + 1, prevPositions) || makeMove(position - 1, prevPositions); return makeMove(position + m, prevPositions) || makeMove(position + 1, prevPositions) || makeMove(position - 1, prevPositions);

This code makes recursive calls to other moves from possible positions and returns true if any of these calls are true. 此代码从可能的位置进行对其他移动的递归调用,如果其中任何一个为true则返回true。 Since you can leap to position+m , then it makes sense that if makeMove(position+m, prevPositions) is true, then makeMove(position, prevPositions) is also true. 由于可以makeMove(position+m, prevPositions)position+m ,因此有意义的是,如果makeMove(position+m, prevPositions)为true,则makeMove(position, prevPositions)也为true。 Similarly you can move to position+1 and position-1 so calls to makeMove of these positions should return the same value as makeMove of your original position. 同样,您可以移至position+1position-1因此对这些位置的makeMove的调用应返回与原始位置的makeMove相同的值。 Hope this made sense! 希望这有道理!

This solution works fine. 此解决方案工作正常。 Please try this. 请尝试这个。 you need to pass, the element array length and the jump values. 您需要传递元素数组长度和跳转值。

public static boolean decideMove(int[] elements, int length, int jump){
    boolean result = false;
    boolean makeMove = true;
    int currentPosition = 0;
        while(makeMove){
            if (currentPosition + jump > length-1){
                return true;
            } 

            if(elements [currentPosition + jump] == 0){
                currentPosition = currentPosition + jump;
                if (currentPosition + jump > length-1){
                    return true;
                } 
            }
            if (elements[currentPosition + 1] == 0){
                currentPosition = currentPosition + 1;
                if (currentPosition + jump > length-1){
                    return true;
                } 
            }

            if(elements[currentPosition +1] ==1 && elements[currentPosition + jump]==1){
                if(elements[currentPosition - 1]==0){
                    currentPosition = currentPosition - 1;
                } else{
                    return false;
                }

            }
        }

    return result;
}

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

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