Tuesday, December 20, 2016

Corners of a square matrix remain corners no matter how many times you invert them

This is a hint That can be used in many programming contests which I noticed during solving the Flipping the matrix problem in Hackerrank.com and thought that it is something that I should write down somewhere. :)
Here is the problem description from Hackerrank.
Sean invented a game involving a  matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times, and the goal of the game is to maximize the sum of the elements in the  submatrix located in the upper-left corner of the  matrix (i.e., its upper-left quadrant).
Given the initial configurations for  matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal. For each matrix, print the maximized sum on a new line.
Input Format
The first line contains an integer, , denoting the number of queries. The subsequent lines describe each of the  queries in the following format:
  1. The first line of each query contains an integer, .
  2. Each line  the  subsequent lines contains  space-separated integers describing the respective values of row  in the matrix.
So first I tried varios thing to reverse the rows and columns in various ways. But later realizd that there should be a simple hint that solve this poblem and there was a one. That is
Corners of a square matrix remain corners no matter how many times you invert them
Using that hint to find the max,

In a 4X4 matrix


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
















Maximum sum of upper left quadrent = Max of blue squares + Max of green squares + Max of gray                                                                   squares + Max of bage squares

Here is the code in C++

No comments:

Post a Comment

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...