Project Euler #11: Largest product in a grid

Sort by

recency

|

113 Discussions

|

  • + 1 comment
    import java.util.*;
    class Solution
    {
        static int[][] mat= new int[20][20];
        static int greatestone=0;
        static void checkVerticalFwd(int x, int y)
        {
            if((x+3)>=20)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x+1][y];
            temp*=mat[x+2][y];
            temp*=mat[x+3][y];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
         static void checkHorizontalFwd(int x, int y)
        {
            if((y+3)>=20)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x][y+1];
            temp*=mat[x][y+2];
            temp*=mat[x][y+3];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
         static void DiagRight(int x, int y)
        {
            if((x+3)>=20|| y + 3 >=20)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x+1][y+1];
            temp*=mat[x+2][y+2];
            temp*=mat[x+3][y+3];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
        static void DiagLeft(int x, int y)
        {
            if((x+3)>=20||(y - 3) < 0)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x+1][y-1];
            temp*=mat[x+2][y-2];
            temp*=mat[x+3][y-3];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
        public static void main(String[] args)
        {
            Scanner scn = new Scanner(System.in);
            
             for(int x = 0; x < 20; x++){
                    for(int y = 0; y < 20; y++){
                        mat[x][y] = scn.nextInt();
                    }
                }
            
                for(int x = 0; x < 20; x++) {
                    for(int y = 0; y < 20; y++) {
                        checkVerticalFwd(x, y);
                        checkHorizontalFwd(x,y);
                        DiagRight(x,y);
                        DiagLeft(x,y) ;
                    }
    								System.out.println(greatestone);
    
            }
            System.out.println(greatestone);
    }
    

    }

  • + 0 comments
    grid = []
    for grid_i in range(20):
    	grid_t = [int(grid_temp) for grid_temp in input().strip().split(' ')]
    	grid.append(grid_t)
    
    len_g = len(grid)
    numbers = []
        
    for i in range(len_g):
        for j in range(len_g):
            #UP
            if (i-3 >= 0):
                number = grid[i][j] * grid[i-1][j] * grid[i-2][j] * grid[i-3][j]
                numbers.append(number)
            #DOWN
            if (i+3 < len_g):
                number = grid[i][j] * grid[i+1][j] * grid[i+2][j] * grid[i+3][j]
                numbers.append(number)
            #LEFT
            if (j-3 >= 0):
                number = grid[i][j] * grid[i][j-1] * grid[i][j-2] * grid[i][j-3]
                numbers.append(number)
            #RIGHT
            if (j+3 < len_g):
                number = grid[i][j] * grid[i][j+1] * grid[i][j+2] * grid[i][j+3]
                numbers.append(number)
            #DIAGONAL
            #DOWN RIGHT
            if (j+3 < len_g and i+3 < len_g):
                number = grid[i][j] * grid[i+1][j+1] * grid[i+2][j+2] * grid[i+3][j+3]
                numbers.append(number)
            #DOWN LEFT
            if (j-3 >= 0 and i+3 < len_g):
                number = grid[i][j] * grid[i+1][j-1] * grid[i+2][j-2] * grid[i+3][j-3]
                numbers.append(number)
            #UP LEFT
            if (j-3 >= 0 and i-3 >= 0):
                number = grid[i][j] * grid[i-1][j-1] * grid[i-2][j-2] * grid[i-3][j-3]
                numbers.append(number)
            #UP RIGHT
            if (j+3 < len_g and i-3 >= 0):
                number = grid[i][j] * grid[i-1][j+1] * grid[i-2][j+2] * grid[i-3][j+3]
                numbers.append(number)
            
    print(max(numbers))
    
  • + 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;
    }
    
  • + 0 comments

    using System;

    class Solution { static void Main() { int[,] grid = ParseInputGrid();

        int maxProduct = FindMaxProduct(grid);
        Console.WriteLine(maxProduct);
    }
    
    static int[,] ParseInputGrid()
    {
        int[,] grid = new int[20, 20];
    
        for (int i = 0; i < 20; i++)
        {
            string[] row = Console.ReadLine().Split(' ');
    
            for (int j = 0; j < 20; j++)
            {
                grid[i, j] = int.Parse(row[j]);
            }
        }
    
        return grid;
    }
    
    static int FindMaxProduct(int[,] grid)
    {
        int maxProduct = 0;
    
        // Check horizontally
        for (int i = 0; i < 20; i++)
        {
            for (int j = 0; j <= 16; j++)
            {
                int product = grid[i, j] * grid[i, j + 1] * grid[i, j + 2] * grid[i, j + 3];
                maxProduct = Math.Max(maxProduct, product);
            }
        }
    
        // Check vertically
        for (int i = 0; i <= 16; i++)
        {
            for (int j = 0; j < 20; j++)
            {
                int product = grid[i, j] * grid[i + 1, j] * grid[i + 2, j] * grid[i + 3, j];
                maxProduct = Math.Max(maxProduct, product);
            }
        }
    
        // Check diagonally (top-left to bottom-right)
        for (int i = 0; i <= 16; i++)
        {
            for (int j = 0; j <= 16; j++)
            {
                int product = grid[i, j] * grid[i + 1, j + 1] * grid[i + 2, j + 2] * grid[i + 3, j + 3];
                maxProduct = Math.Max(maxProduct, product);
            }
        }
    
        // Check diagonally (top-right to bottom-left)
        for (int i = 0; i <= 16; i++)
        {
            for (int j = 3; j < 20; j++)
            {
                int product = grid[i, j] * grid[i + 1, j - 1] * grid[i + 2, j - 2] * grid[i + 3, j - 3];
                maxProduct = Math.Max(maxProduct, product);
            }
        }
    
        return maxProduct;
    }
    

    }

  • + 0 comments
    public static int largestProductGrid(int[][] grid) {
            int max = 0, product;
            for (int rows = 0; rows <= 19; rows++) {
                for (int columns = 0; columns <= 19; columns++) {
                    if (columns + 3 <= 19) {
                        product = grid[rows][columns] * grid[rows][columns + 1] * grid[rows][columns + 2] * grid[rows][columns + 3];
                        if (product > max) {
                            max = product;
                        }
                    }
                    if (rows + 3 <= 19) {
                        product = grid[rows][columns] * grid[rows + 1][columns] * grid[rows + 2][columns] * grid[rows + 3][columns];
                        if (product > max) {
                            max = product;
                        }
                    }
                    if (columns + 3 <= 19 && rows + 3 <= 19) {
                        product = grid[rows][columns] * grid[rows + 1][columns + 1] * grid[rows + 2][columns + 2] * grid[rows + 3][columns + 3];
                        if (product > max) {
                            max = product;
                        }
                    }
                    if (rows + 3 <= 19 && columns - 3 >= 0) {
                        product = grid[rows][columns] * grid[rows + 1][columns - 1] * grid[rows + 2][columns - 2] * grid[rows + 3][columns - 3];
                        if (product > max) {
                            max = product;
                        }
                    }
                }
            }
            return max;
        }