The game of grid

3.7

7 votes
Implementation, Basic Programming, Medium, approximate, Basics of Implementation
Problem

You are given a grid of size N×N. Each cell of the grid is colored in either white or black. You have to extract some subgrids out of the original grid. To extract these subgrids, you are required to follow these constraints:

  1. All the subgrids should be non-overlapping in nature.
  2. All the subgrids should be of different sizes. Two grid sizes a×b and c×d are considered to be different if a!=c or b!=d.

The score of a subgrid is defined by the absolute difference between the count of white and black cells in it. Your task is to maximize the sum of the scores of all the selected subgrids. It is not necessary to select all the subgrids of the grid.

Input format

  • First line: N that denotes the size of the grid
  • Next N lines: N integers where each integer is either 0 or 1 making it a matrix of size N×N

Output format

  • First line: An integer X that denotes the number of subgrids that you have to select
  • Next X lines: Four integers each a,b,c, and d that denote the selected subgrid belongs to the rows in the range of a to c and columns in the range of b to d

Note:

  • The subgrids should not overlap with each other.
  • The integers a,b,c, and d should abide by the following constraints: 1acN, 1bdN .

Constraints
1N1000
Note: The input data contains only 50 percent of the original test data. The other data will be added after the contest and the submissions will be rejudged.

Verdict and scoring
If any of the grids overlap or the selected submatrix is not valid, then you will get a Wrong Answer verdict. For each of the valid output, your result is the sum of the scores of all the selected submatrices. Each submatrix score is the absolute difference of the count of black and white cells in it.

Sample Input
3
0 0 0
0 0 1
1 0 0
Sample Output
2
1 1 2 3
3 1 3 3
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the given sample if you pick the submatrices as shown in the output then your total score will be (61)+(31)=7

Editor Image

?