Bob is playing a game on a 2D grid with $$$n$$$ rows and $$$m$$$ columns. He starts at the top-left corner, cell $$$(1, 1)$$$, and his goal is to reach the bottom-right corner, cell $$$(n, m)$$$.
Bob can move in the following ways:
- Choose an index $$$i$$$ ($$$1 \le i \le k$$$) and:
- Move down by $$$A[i]$$$ rows: from $$$(x, y)$$$ to $$$(x + A[i], y)$$$
- Move right by $$$B[i]$$$ columns: from $$$(x, y)$$$ to $$$(x, y + B[i])$$$
- Move diagonally down-right by $$$C[i]$$$ cells: from $$$(x, y)$$$ to $$$(x + C[i], y + C[i])$$$
Each such move has a cost of $$$1$$$.
You are also guaranteed that each of the arrays $$$A$$$,$$$B$$$ and $$$C$$$ contain $$$1$$$.
You are given the integers $$$n$$$, $$$m$$$, $$$k$$$ and the arrays $$$A$$$, $$$B$$$, and $$$C$$$, each of length $$$k$$$. Your task is to compute the minimum cost to reach cell $$$(n, m)$$$ starting from $$$(1, 1)$$$.
$$$n,m,k\leq 10^3$$$








Auto comment: topic has been updated by haitax511 (previous revision, new revision, compare).
Auto comment: topic has been updated by haitax511 (previous revision, new revision, compare).
won't just dp[i][j] work here for each operation? (in $$$O(n^2 \times m)$$$) also i guess you can model it was a graph and do BFS, there can be at most $$$n^2 \times m^2$$$ edges (which is tight but passable)
If m = 1 you basically have the coin change problem with where you are at (1, 1) and have to move to (n, 1) in minimum amount of moves using allowed moves. The better known solution for this problem is
O(n*k), so I guess we can't do much better thanO(n*m*k)(basically doing a dp and checking all posible transitions on every cell). One thing we can notice is that the answer is ≤ n + m — 2, so maybe a BFS would be better in the worst possible case:The idea here is: if
kis big the BFS will stop at an early distance (compensating the extra cost of checking a lot of transitions). Ifkis small than we haveO(E) = O(n*m*k)which should be ok sincekis small. I am not sure this actually works though LOLCompute the following in $$$O(n\cdot k) + O(m\cdot k) + O(min(n, m) \cdot k)$$$:
Then the answer is the minimum of dp_diagonal[i] + dp_down[n — i] + dp_right[m — i] for $$$0 \leq i \leq min(n, m)$$$. Overall time complexity: $$$O((n + m) \cdot k)$$$.