CSES - Datatähti 2023 alku - Results
Submission details
Task:Ruudukko
Sender:Sup
Submission time:2022-11-10 18:27:58 +0200
Language:C++ (C++11)
Status:READY
Result:61
Feedback
groupverdictscore
#1ACCEPTED28
#2ACCEPTED33
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.01 s1, 2, 3details
#3ACCEPTED0.01 s1, 2, 3details
#4ACCEPTED0.02 s2, 3details
#5ACCEPTED0.03 s2, 3details
#6ACCEPTED0.04 s2, 3details
#7--3details
#8--3details
#9--3details

Code

// CPP program to count number
// of decreasing path in a matrix
//#include <bits/stdc++.h>
// using namespace std;
//#include <iostream>


#include <bits/stdc++.h>
#include <cmath>
using namespace std;

#define MAX 1000

// Function that returns the number of
// decreasing paths from a cell(i, j)
int CountDecreasingPathsCell(int(&mat)[MAX][MAX], int(&dp)[MAX][MAX],
                                              int n, int x, int y, int (&delta)[4*MAX][2], int modulo_num)
{
    // checking if already calculated
    if (dp[x][y] != -1)
        return dp[x][y];
 
    // all possible paths
    //int delta[4][2] = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };
    
    
    int newx, newy;
 
    // counts the total number of paths
    int ans = 1;

    
 
    // In all four allowed direction.
    for (int i = 0; i < 4*n; i++) {
 
        // new co-ordinates
        newx = x + delta[i][0];
        newy = y + delta[i][1];
 
        // Checking if not going out of matrix and next
        // cell value is less than current cell value.
        if (newx >= 0 && newx < n && newy >= 0
            && newy < n && mat[newx][newy] < mat[x][y]) {
            ans += CountDecreasingPathsCell(mat, dp, n, newx, newy, delta, modulo_num);
            if (ans >= modulo_num){
                ans = ans % modulo_num;
            }
        }
    }
    // function that returns the answer
    return dp[x][y] = ans;
}
 
// Function that counts the total
// decreasing path in the matrix
int countDecreasingPathsMatrix(int n,
                               int(&mat)[MAX][MAX], int(&delta)[4*MAX][2], int modulo_num)
{
    int dp[MAX][MAX];
 
    // Initialising dp[][] to -1.
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            dp[i][j] = -1;
        }
    }
 
    int sum = 0;


 
    // Calculating number of decreasing path from each cell.
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            sum += CountDecreasingPathsCell(mat, dp, n, i, j, delta, modulo_num);
            if (sum >= modulo_num){
                sum = sum % modulo_num;
            }
            //sum += CountDecreasingPathsCell(mat, dp, n, i, j) % modulo_num;
        }
    }
    return sum;
}





// Driver Code
int main()
{
    //int i = 1;
    int number_of_rows;
    cin >> number_of_rows; 
    //int input_row[number_of_rows] = {};
    int input_row1;
    //string replace_char = ",";


    //int mat[1000][1000];

    int modulo_num = pow(10, 9) + 7;

    //int j;

    int delta[4*MAX][2];
    int C = 0;
    for (int i = -number_of_rows; i<=number_of_rows; i++){
        if(i != 0){
            delta[C][0] = i;
            delta[C][1] = 0;
            C++;
        }
    }
    C++;
    for (int i = -number_of_rows; i<=number_of_rows; i++){
        if(i != 0){
            delta[C][0] = 0;
            delta[C][1] = i;
            C++;
        }
    }


    int my_array[MAX][MAX];

    //cout << number_of_rows << endl;

    for (int iii = 0; iii < number_of_rows; iii++)
    {
        for (int ii = 0; ii < number_of_rows; ii++)
        {
            cin >> input_row1;

            //input_row[ii] = input_row1;
            //cout << input_row1 << "its the input row" << endl;

            my_array[iii][ii] = input_row1;

            //char *modified_input = strtok(input_row, " ");
            //cout << modified_input << "its modified_input" << endl;
            //int number = stoi(modified_input);
            //cout << number << "its the number" << endl;
            //my_array[ii] = number;
            //cout << ii << "its the ii number" << endl;
            //cout << iii << "its the iii number----" << endl;

        }

        //my_array[iii] = input_row;
    }



    //int arrSize = sizeof(my_array)/sizeof(my_array[0]);
    //cout << arrSize << "its the length of arrSize" << endl;
    //cout << input_row[0] << "its the input_row---" << endl;
    //cout << input_row[1] << "its the input_row---" << endl;
    //cout << input_row[2] << "its the input_row---" << endl;

    //cout << my_array[0][0] << "its the my_array " << endl;
    //cout << my_array[1][1] << "its the my_array " << endl;

    //std::copy_n(std::istream_iterator<int>(std::cin), number_of_rows, my_array);

    //int mat[100][100] = { { 2, 1, 3 }, { 1, 1, 1 }, {9, 2, 7} };     // function call that returns the

    //cout << mat[50][0] << "its the mat print" << endl;
    //cout << modulo_num << "its the modulo_num" << endl;

    //int modulo_num = pow(10, 9)+7;


    int number_of_paths = countDecreasingPathsMatrix(number_of_rows, my_array, delta, modulo_num);
    cout << number_of_paths << endl;      // count of decreasing paths in a matrix
    return 0;
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
1 1 1
1 1 1
1 1 1

correct output
9

user output
9

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
1 2 3
6 5 4
7 8 9

correct output
135

user output
135

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
7 8 1
4 5 4
3 9 6

correct output
57

user output
57

Test 4

Group: 2, 3

Verdict: ACCEPTED

input
100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
10000

user output
10000

Test 5

Group: 2, 3

Verdict: ACCEPTED

input
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
187458477

user output
187458477

Test 6

Group: 2, 3

Verdict: ACCEPTED

input
100
2995 8734 1018 2513 7971 5063 ...

correct output
964692694

user output
964692694

Test 7

Group: 3

Verdict:

input
1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1000000

user output
(empty)

Test 8

Group: 3

Verdict:

input
1000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
229147081

user output
(empty)

Test 9

Group: 3

Verdict:

input
1000
520283 805991 492643 75254 527...

correct output
951147313

user output
(empty)