Good Grass
InterviewTime limit1sMemory limit128 MB
Find the 3x3 subgrid with the largest sum in a grid of milk values and report that sum with the upper-left corner, breaking ties by smallest row then column.
- Level
Easy3 of 10
- Topics
- Array, Prefix sum, Brute force, Implementation
- Solved
- No attempts yet
Problem
Bessie believes that somewhere in her pasture there is a patch of very special grass, the best grass on earth. Cows that eat it produce more milk.
The pasture is a fully populated rectilinear grid with rows and columns, with one cow in every cell, where and . The milk production of the cow in cell is , with .
Bessie wants to locate the special grass by finding the square of cells whose total milk production is the largest.
Among all squares in the grid, find the one whose nine cells have the greatest sum, and output that sum together with the position of its upper-left cell (row first, then column). Rows and columns are numbered starting from 1.
If several squares share the same greatest sum, output the one with the smallest row number. If there is still a tie, output the one with the smallest column number.
Input
- Line 1: Two space-separated integers and .
- Lines 2 through : Line contains space-separated integers representing row of the pasture grid.
Output
- Line 1: A single integer, the greatest possible sum of a square.
- Line 2: Two space-separated integers, the row and column of the upper-left corner of the best square.