Can anyone help me understand this piece of code?

argo

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. 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. 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. That means you can move from position x to x+1, x−1 or x+m in one move. The new position must contain 0. Also you can move to any position greater than n-1.

You can't move backward from position 0. If you move to any position greater than n−1, you win the game.

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

   0 0 0 1 0 0

Output: Yes

input: 6 2

   0 0 1 1 0 0

Output: No

deepmindz

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.

The makeMove method goes through a few base cases to see if a branch of recursion has failed or succeeded.

1.) 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.

2.) 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. -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

3.) 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. 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. 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. Hope this made sense!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using yield and zip on python. Can anyone help me understand this piece of code?

From Dev

Can anyone help me to understand typedef in this program?

From Dev

Need Help to understand the piece of code

From Dev

Can someone help me to understand output of this code

From Dev

I dont't understand why im getting syntax error on my code below. Can anyone help me?

From Dev

Can anyone help me out to try pagination for the below code...!

From Dev

Whats wrong with this php code?Can anyone help me

From Dev

Can anyone help me to find what is wrong in below code?

From Dev

Error Code 13 in Mysql on UNIX. Can anyone Help me?

From Dev

Can anyone help me out to try pagination for the below code...!

From Dev

Can anyone help me with these minidumps?

From Dev

Can anyone help me with this javascript?

From Dev

Please help me with the following piece of code?(JAVA)

From Dev

Please help me with the following piece of code?(JAVA)

From Java

Can anyone help me understand why im getting this runtime error with my leetcode problem? Longest Prefix

From Dev

can someone help me understand why VS Code is throwing this error?

From Dev

can someone help me understand why VS Code is throwing this error?

From Dev

Can anyone help me with Linq Sorting

From Dev

Can anyone help me on this Link issue:

From Dev

Can anyone help me to optimize my query?

From Dev

Can anyone help me to find a error in PHP?

From Dev

Can anyone help me to optimize my query?

From Dev

Can anyone help me to make require working?

From Dev

Can anyone help me with this simple For Loop?

From Dev

Can anyone help me in the below scenario in Talend

From Dev

Can anyone help me with this python program?

From Dev

Can anyone help me with this drawback with Hibernate and JSF?

From Dev

Can anyone help me fix grub?

From Dev

I need help trying to understand this piece of code about structures and pointers

Related Related

  1. 1

    Using yield and zip on python. Can anyone help me understand this piece of code?

  2. 2

    Can anyone help me to understand typedef in this program?

  3. 3

    Need Help to understand the piece of code

  4. 4

    Can someone help me to understand output of this code

  5. 5

    I dont't understand why im getting syntax error on my code below. Can anyone help me?

  6. 6

    Can anyone help me out to try pagination for the below code...!

  7. 7

    Whats wrong with this php code?Can anyone help me

  8. 8

    Can anyone help me to find what is wrong in below code?

  9. 9

    Error Code 13 in Mysql on UNIX. Can anyone Help me?

  10. 10

    Can anyone help me out to try pagination for the below code...!

  11. 11

    Can anyone help me with these minidumps?

  12. 12

    Can anyone help me with this javascript?

  13. 13

    Please help me with the following piece of code?(JAVA)

  14. 14

    Please help me with the following piece of code?(JAVA)

  15. 15

    Can anyone help me understand why im getting this runtime error with my leetcode problem? Longest Prefix

  16. 16

    can someone help me understand why VS Code is throwing this error?

  17. 17

    can someone help me understand why VS Code is throwing this error?

  18. 18

    Can anyone help me with Linq Sorting

  19. 19

    Can anyone help me on this Link issue:

  20. 20

    Can anyone help me to optimize my query?

  21. 21

    Can anyone help me to find a error in PHP?

  22. 22

    Can anyone help me to optimize my query?

  23. 23

    Can anyone help me to make require working?

  24. 24

    Can anyone help me with this simple For Loop?

  25. 25

    Can anyone help me in the below scenario in Talend

  26. 26

    Can anyone help me with this python program?

  27. 27

    Can anyone help me with this drawback with Hibernate and JSF?

  28. 28

    Can anyone help me fix grub?

  29. 29

    I need help trying to understand this piece of code about structures and pointers

HotTag

Archive