# Program to count number of ways ball can drop to lowest level by avoiding blacklisted steps in Python

Suppose we have a value h and a list of numbers called blacklist. We are currently at height h, and are playing a game to move a small ball down to height 0. Now, in even rounds (starting from 0) we can move the ball 1, 2, or 4 stairs down. And in odd rounds, we can move the ball 1, 3, or 4 stairs down. Some levels are blacklisted. So if the ball reach there, it will die immediately. We have to find the number of ways the ball can move down at height 0. If the answer is too large, then mod the result by 10^9 + 7.

So, if the input is like h = 5 blacklist = [2, 1], then the output will be 2, because on round 0, move one step first (5 to 4), then in the next round from 4 to 0. Another possible way could be at round 0, move two steps (5 to 3), then in the next round 3 to 0.

To solve this, we will follow these steps −

• blacklist := a new set from the elements of blacklist
• if 0 is in blacklist or h is in blacklist, then
• return 0
• dp := a list of size h, and inside that store pairs [0, 0] at each index
• dp[0] := [1, 1]
• m := 10^9 + 7
• for i in range 1 to h, do
• for each x in [1, 2, 3, 4], do
• if i - x >= 0 and i - x is not in blacklist, then
• if x is not same as 3, then
• dp[i, 0] := dp[i, 0] + dp[i - x, 1]
• if x is not same as 2, then
• dp[i, 1] := dp[i, 1] + dp[i - x, 0]
• dp[i, 0] := dp[i, 0] mod m
• dp[i, 1] := dp[i, 1] mod m
• return dp[h, 0]

## Example

Let us see the following implementation to get better understanding −

def solve(h, blacklist):
blacklist = set(blacklist)
if 0 in blacklist or h in blacklist:
return 0
dp = [[0, 0] for i in range(h + 1)]
dp[0] = [1, 1]
m = 10 ** 9 + 7
for i in range(1, h + 1):
for x in [1, 2, 3, 4]:
if i - x >= 0 and i - x not in blacklist:
if x != 3:
dp[i][0] += dp[i - x][1]
if x != 2:
dp[i][1] += dp[i - x][0]
dp[i][0] %= m
dp[i][1] %= m
return dp[h][0]

h = 5
blacklist = [2, 1]
print(solve(h, blacklist))

## Input

5, [2, 1]

## Output

2