edwardpv's blog

By edwardpv, history, 7 years ago, In English

I was reading the tutorial on hackerearth about 2-d dp, and i read got stuck with this question. Does the question mean that the boy and girl should meet at one point and also they should not traverse the cell already reached by the other player. If not then why is the outer rows and columns not taken in consideration. Also a little discussion on the calculation of matrices will be helpful for me.

Link to Problem(above edit distance-special variant)

"You are given a 2-D matrix A of n rows and m columns where A[i][j] denotes the calories burnt. Two persons, a boy and a girl, start from two corners of this matrix. The boy starts from cell (1,1) and needs to reach cell (n,m). On the other hand, the girl starts from cell (n,1) and needs to reach (1,m). The boy can move right and down. The girl can move right and up. As they visit a cell, the amount in the cell A[i][j] is added to their total of calories burnt. You have to maximize the sum of total calories burnt by both of them under the condition that they shall meet only in one cell and the cost of this cell shall not be inclu``ded in either of their total."

#include <bits/stdc++.h>
    #define F(i,a,b) for(int i = (int)(a); i <= (int)(b); i++)
    #define RF(i,a,b) for(int i = (int)(a); i >= (int)(b); i--)
    #define MAX 1005
    int Boy1[MAX][MAX];
    int Boy2[MAX][MAX];
    int Girl1[MAX][MAX];
    int Girl2[MAX][MAX];
    using namespace std;
    int main()
    {
        int N,M,ans,op1,op2;
        scanf("%d%d",&N,&M);
        int Workout[MAX][MAX];
        ans = 0;

        //Take input the calories burnt matrix
        F(i,1,N)
            F(j,1,M)
                scanf("%d",&Workout[i][j]);

        //Table for Boy's journey from start to meeting cell
        F(i,1,N)
            F(j,1,M)
                Boy1[i][j] = max(Boy1[i-1][j],Boy1[i][j-1]) + Workout[i][j];

        //Table for boy's journey from end to meet cell
        RF(i,N,1)
            RF(j,M,1)
                Boy2[i][j] = max(Boy2[i+1][j],Boy2[i][j+1]) + Workout[i][j];


        //Table for girl's journey from start to meeting cell
        RF(i,N,1)
            F(j,1,M)
                Girl1[i][j] = max(Girl1[i+1][j],Girl1[i][j-1]) + Workout[i][j];


        //Table for girl's journey from end to meeting cell
        F(i,1,N)
            RF(j,M,1)
                Girl2[i][j] = max(Girl2[i-1][j],Girl2[i][j+1]) + Workout[i][j];


        //Now iterate over all meeting positions (i,j)
        F(i,2,N-1)
        {
            F(j,2,M-1)
            {
                //For the option 1
                op1 = Boy1[i][j-1] + Boy2[i][j+1] + Girl1[i+1][j] + Girl2[i-1][j];

                //For the option 2
                op2 = Boy1[i-1][j] + Boy2[i+1][j] + Girl1[i][j-1] + Girl2[i][j+1];

                //Take the maximum of two options at each position
                ans = max(ans,max(op1,op2));
            }
        }

        printf("%d",ans);
        return 0;
    }

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Boy's and girl's paths must intersect only in one cell. There are two cases, and it is obvious that the meeting cell mustn't be at the edge.