Cerinta completa

You are situated in an dimensional grid at position . The dimensions of the grid are . In one step, you can walk one step ahead or behind in any one of the dimensions. This implies that there are always possible moves if movements are unconstrained by grid boundaries. How many ways can you take steps without leaving the grid at any point? You leave the grid if at any point , either or .

For example, you start off in a 3 dimensional grid at position . The dimensions of the grid are , so each of your axes will be numbered from to . If you want to move step, you can move to the following coordinates: .

image

If we started at in the same grid, our new paths would lead to . Other moves are constrained by .

Function Description

Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo .

gridWalking has the following parameter(s):

  • m: an integer that represents the number of steps
  • x: an integer array where each represents a coordinate in the dimension where
  • D: an integer array where each represents the upper limit of the axis in the dimension

Input Format

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

Each of the next sets of lines is as follows:

  • The first line contains two space-separated integers, and .
  • The next line contains space-separated integers .
  • The third line of each test contains space-separated integers .

Constraints

Output Format

Output one line for each test case. Since the answer can be really huge, output it modulo .

Sample Input

1
2 3
1 1
2 3

Sample Output

12

Explanation

We are starting from (1, 1) in a 2-D grid, and need to count the number of possible paths with length equal to .

Here are the paths:













Limbajul de programare folosit: cpp14

Cod:

// https://www.hackerrank.com/challenges/grid-walking

#include <iostream>
#include <vector>

using namespace std;

#define MOD 1000000007

int getWays(vector<int> x, vector<int> d, int M) {
    int N = d.size();

    long long md[M + 1][N + 1];
    for (int i = 0; i < N; i++) {
        long long m[d[i] + 1][M + 1];
        int D = d[i];

        for (int j = 1; j <= D; j++)
            m[j][0] = 1;

        for (int n = 1; n <= M; n++) {
            for (int j = 1; j <= D; j++) {
                m[j][n] = 0;
                if (j - 1 > 0)
                    m[j][n] = (m[j][n] + m[j - 1][n - 1]) % MOD;
                if (j + 1 <= D)
                    m[j][n] = (m[j][n] + m[j + 1][n - 1]) % MOD;
            }
        }

        md[0][i + 1] = 1;
        for (int j = 1; j <= M; j++)
            md[j][i + 1] = m[x[i]][j];
    }

    long long c[M + 1][M + 1];
    for (int i = 0; i <= M; i++) {
        c[i][0] = 1;
        c[i][i] = 1;
    }
    for (int i = 1; i <= M; i++)
        for (int j = 1; j < i; j++)
        c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;

    long long mdt[M + 1][N + 1];
    for (int i = 0; i <= M; i++)
        mdt[i][1] = md[i][1];
    for (int i = 0; i <= N; i++)
        mdt[0][i] = 1;

    for (int i = 2; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            mdt[j][i] = 0;
            for (int k = 0; k <= j; k++)
                mdt[j][i] =
                    (mdt[j][i] +
                     ((c[j][j - k] *
                       ((mdt[k][i - 1] * md[j - k][i]) % MOD)) % MOD)) % MOD;
        }
    }

    return mdt[M][N];
}

int main() {
    int T, N, M, temp;
    cin >> T;

    for (int i = 0; i < T; i++) {
        cin >> N >> M;
        vector<int> x;
        vector<int> d;

        for (int j = 0; j < N; j++) {
            cin >> temp;
            x.push_back(temp);
        }

        for (int j = 0; j < N; j++) {
            cin >> temp;
            d.push_back(temp);
        }

        cout << getWays(x, d, M) << endl;
    }

    return 0;
}

Scor obtinut: 1.0

Submission ID: 464612730

Link challenge: https://www.hackerrank.com/challenges/grid-walking/problem

Grid Walking