2

A robot is moving in the (x~y) coordinate system according to a plan that was attached to him prior to his start. The robot always starts at (0, 0) and only understands 4 commands - WEST - (x-1, y) SUD - (x, y-1) OST - (x+1, y) NORD - (x, y+1) Every command lets the robot move one unit in the chosen direction. The robot's attached plan is made of a sequence of these commands.

Given a random coordinate for (x, y) and a natural number K, we want to calculate the number of different plans in the length of K that would bring the robot from (0, 0) to (x, y).

Aside of an explanation, I need an algorithm and the pseudo-code for it preferrably in C

So far, only thing I could think about is that the main condition would be that K cannot be smaller than abs(x+y), in this case i'd need to return 0 since there are zero sequences of K length that will get me to (x,y), or, more accurately, i'll never be able to get there with K steps.

natik
  • 21
  • 3
  • Can the coordinates along the path be negative? – Solomon Ucko Apr 25 '23 at 19:18
  • 2
    The distance to the goal is the [manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry), which is `|x1-x2| + |y1-y2|`. If a move would put the robot at a location where the distance to the goal is greater than the number of remaining moves, then that move is invalid. – user3386109 Apr 25 '23 at 19:26
  • 2
    Side note: the parity of `x+y` must be equal to the parity of `K`. If not, no solution. – Damien Apr 25 '23 at 19:44
  • 1
    Solomon yes. Damien, i don't need a solution to happen though, I need to return the amount of different sequences of commands to get there. – natik Apr 25 '23 at 21:13

1 Answers1

0

Use dynamic programming with the following observation: f(x, y, K) = f(x - 1, y, K - 1) + f(x + 1, y, K - 1) + f(x, y - 1, K - 1) + f(x, y + 1, K - 1)

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45