# Grid Walking

### Problem Statement :

```You are situated in an n dimensional grid at position (x[1],x[2],...,x[n]). The dimensions of the grid are D[1],D[2],...,D[n]). In one step, you can walk one step ahead or behind in any one of the n dimensions. This implies that there are always 2*n possible moves if movements are unconstrained by grid boundaries. How many ways can you take m steps without leaving the grid at any point? You leave the grid if at any point x[i], either x[i] <= 0 or x[i] >D[i].

For example, you start off in a 3 dimensional grid at position x = [2,2,2]. The dimensions of the grid are D = [3,3,3], so each of your axes will be numbered from 1 to 3. If you want to move m =1 step, you can move to the following coordinates: {[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]}.

image
If we started at x=[1,1,1] in the same grid, our new paths would lead to {[1,1,2],[1,2,1],[2,1,1]}. Other moves are constrained by x[i] !<= 0.

Function Description

Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo (10^9+7).

gridWalking has the following parameter(s):

m: an integer that represents the number of steps
x: an integer array where each x[i] represents a coordinate in the ith dimension where 1 <= i <=n
D: an integer array where each D[i] represents the upper limit of the axis in the ith dimension
Input Format

The first line contains an integer t, the number of test cases.

Each of the next t sets of lines is as follows:

The first line contains two space-separated integers, n and m.
The next line contains n space-separated integers x[i].
The third line of each test contains n space-separated integers D[i].

Constraints
1 <= t <= 10
1 <= n <= 10
1 <= m <= 300
1 <= D[i] <=100
1 <= x[i] <=D[i]

Output Format

Output one line for each test case. Since the answer can be really huge, output it modulo 10^9 + 7.```

### Solution :

```                            ```Solution in C :

In C++ :

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
#include <iostream>
#include <memory.h>

const int maxn = 10;
const int maxm = 10000;
const long long modulo = 1000000007;

int D[maxn];
int X[maxn];

int n,m;
int solve(){
std::cin>>n>>m;
for (int i=0; i<n; i++)
std::cin>>X[i];
for (int i=0; i<n; i++)
std::cin>>D[i];

long long totalway[n][m+1];
for (int i=0; i<n; i++){
long long total[D[i]];
memset(total, 0, sizeof(total));
total[X[i]-1] = 1;
long long ans;
totalway[i][0] = 1;
for (int step=1; step<=m; step++){
long long tmp[D[i]];
for (int j=0; j<D[i]; j++){
tmp[j] = 0;
if (j>0) tmp[j] += total[j-1];
if (j<D[i]-1) tmp[j] += total[j+1];
tmp[j] %= modulo;
}
totalway[i][step] = 0;
for (int j=0; j<D[i]; j++){
total[j] = tmp[j];
totalway[i][step] += total[j];
totalway[i][step] %= modulo;
}
}
}

long long C[m+1][m+1];
for (int i=0; i<=m; i++){
C[i][0] = 1;
for (int j=1; j<=i; j++)
C[i][j] = (C[i-1][j]+C[i-1][j-1])%modulo;
for (int j=i+1; j<=m; j++)
C[i][j] = 0;
}

long long result[n][m+1];
for (int i=0; i<n; i++)
for (int step=0; step<=m; step++){
if (i==0){
result[i][step] = totalway[i][step];
continue;
}
result[i][step] = 0;
for (int k=0; k<=step; k++){
long long tmp = (result[i-1][k]*totalway[i][step-k])%modulo;
tmp = tmp*C[step][step-k]%modulo;
result[i][step] += tmp;
result[i][step] %= modulo;
}

}
std::cout<<result[n-1][m]<<std::endl;

}
int main(){
int T;
std::cin>>T;
while (T>0){
solve();
T--;
}
return 0;
}

In Java :

import java.io.*;
import java.util.*;

public class Solution {

public static final int MOD = 1000000007;
public static final int MAXSTEPS = 310;

public static void main(String[] args) throws IOException{
long[][] pascals = new long[MAXSTEPS][];
for (int i=0; i<MAXSTEPS; i++){
pascals[i] = new long[i+1];
pascals[i][0] = 1;
for (int j=1; j<pascals[i].length; j++){
pascals[i][j] = pascals[i-1][j-1];
if (j<pascals[i-1].length)
pascals[i][j] += pascals[i-1][j];
pascals[i][j] %= MOD;
}
}

for (int test = 0; test<cases; test++){
int dims = Integer.parseInt(st.nextToken());
int steps = Integer.parseInt(st.nextToken());
int[] starts = new int[dims];
for (int i=0; i<dims; i++)
starts[i] = Integer.parseInt(st.nextToken());
int[] bounds = new int[dims];
for (int i=0; i<dims; i++)
bounds[i] = Integer.parseInt(st.nextToken());

long[] numWays = new long[steps+1];
numWays[0] = 1;
for (int i=0; i<dims; i++){
long[] tempWays = new long[numWays.length];
long[] nextDimWays = numPossSteps(starts[i], bounds[i], steps);

for (int j=0; j<tempWays.length; j++){
for (int k=0; k<=j; k++){
tempWays[j] %= MOD;
}
}
numWays = tempWays;
}

System.out.println(numWays[steps]);
}
}

public static long[] numPossSteps(int start, int bound, int steps){
long[] ret = new long[steps+1];
ret[0] = 1;
long[] trackLoc = new long[bound];
trackLoc[start-1] = 1;
for (int i=1; i<ret.length; i++){
long[] nextTrack = new long[trackLoc.length];
for (int j=0; j<trackLoc.length; j++){
if (j>0)
nextTrack[j] += trackLoc[j-1];
if (j+1<bound)
nextTrack[j] += trackLoc[j+1];
nextTrack[j] %= MOD;
}
trackLoc = nextTrack;
ret[i] = sum(trackLoc);
}
return ret;
}

public static long sum(long[] array){
long sum = 0;
for (int i=0; i<array.length; i++){
sum+=array[i];
sum %= MOD;
}
return sum;
}
}

In C :

typedef long long int LL;
int mod=(int)(1e9+7);
int ways[302][102],fact[501],mi[501];
LL dp[12][302],tmp;
int x[12],d[12],i,j,k,t,n,m;
int res;
int raise(int pow,int power){
res=1;
while(power){
if(power&1) res=((LL)res*pow)%mod;
pow=((LL)pow*pow)%mod;
power>>=1;
}
return res;
}
int modularInverse(int num){
return raise(fact[num],mod-2);
}
int main(){
fact[0]=1;
for(i=1;i<=300;i+=1) fact[i]=((LL)fact[i-1]*i)%mod;
mi[0]=1;
for(i=1;i<=300;i+=1) mi[i]=modularInverse(i);
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i+=1) scanf("%d",&x[i]);
for(i=1;i<=n;i+=1) scanf("%d",&d[i]);
dp[0][0]=1;
for(k=1;k<=n;k+=1){
for(j=1;j<=d[k];j+=1) ways[0][j]=1;
for(i=1;i<=m;i+=1){
for(j=1;j<=d[k];j+=1){
ways[i][j]=0;
if(j>1)
ways[i][j]+=ways[i-1][j-1];
if(ways[i][j]>=mod) ways[i][j]-=mod;
if(j<d[k])
ways[i][j]+=ways[i-1][j+1];
if(ways[i][j]>=mod) ways[i][j]-=mod;
}
}
for(i=0;i<=m;i+=1){
dp[k][i]=0;
for(j=0;j<=i;j+=1){
tmp=(dp[k-1][i-j]*ways[j][x[k]])%mod;
dp[k][i]=(dp[k][i]+(tmp*mi[j])%mod);
if(dp[k][i]>=mod) dp[k][i]%=mod;
}
}
}
printf("%lld\n",(dp[n][m]*fact[m])%mod);
}
return 0;
}

In Python3 :

T = int(input())
MOD = 1000000007

C = [[0] * 400 for _ in range(400)]

C[0][0] = 1
for i in range(400):
C[i][0], C[i][i] = 1, 1
for j in range(1, i):
C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD

for t in range(T):
N, M = [int(c) for c in input().split()]
X = [int(c) for c in input().split()]
D = [int(c) for c in input().split()]

# dp[N][D[i]][M+1]
dp = [[[0] * (M+1) for j in range(D[i])] for i in range(N)]

for i in range(N):
for j in range(D[i]):
dp[i][j][0] = 1

if j > 0:
dp[i][j][1] += 1
if j < D[i] - 1:
dp[i][j][1] += 1

for m in range(2, M+1):
for j in range(D[i]):
if j > 0:
dp[i][j][m] += dp[i][j-1][m-1]
if j < D[i] - 1:
dp[i][j][m] += dp[i][j+1][m-1]

total = [dp[0][X[0]-1][m] for m in range(M+1)]

for i in range(1, N):
for j in reversed(range(1, M + 1)):
total[j] = sum(total[k] * C[j][k] * dp[i][X[i]-1][j-k] for k in range(j+1))

print(total[M] % MOD)```
```

## Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

## Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f