Project Euler #11: Largest product in a grid

  • + 0 comments
    vector<vector<int>> vd = {{0,0},{1,1},{2,2},{3,3}};
    vector<vector<int>> vd2 = {{0,0},{1,-1},{2,-2},{3,-3}};
    vector<vector<int>> vlr = {{0,0},{0,1},{0,2},{0,3}};
    vector<vector<int>> vtb = {{0,0},{1,0},{2,0},{3,0}};
    ll getD(vector<vector<int>> &v,int i,int j)
    {
        vector<vector<vector<int>>> vList = {vd,vd2,vlr,vtb};
        ll mx = 0;
        for(int m = 0;m<vList.size();m++)
        {
                ll prod = 1;
                for(int k = 0;k<4;k++)
                {
                    int fi = i+vList[m][k][0];
                    int fj = j+vList[m][k][1];
                    if(fi<20 && fj<20 && fj>=0) 
                    prod = prod * (ll)v[fi][fj];
                    else 
                    {
                        prod = 0; break;
                    }
                }
                mx = max(mx,prod); 
        }
        return mx;
    }
    ll greatestProd(vector<vector<int>> &v)
    {
        ll mx = 0;
        for(int i = 0;i<20;i++)
        {
            for(int j = 0;j<20;j++)
            {
                mx = max(mx,getD(v, i, j));
            }
        }
        return mx;
    }