Flipping the Matrix


Problem Statement :


Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n * n submatrix located in the upper-left quadrant of the matrix.

Given the initial configurations for q 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.

Example
 matrix = [[1,2],[3,4]]
1 2
3 4
It is 2*2 and we want to maximize the top left quadrant, a 1*1 matrix. Reverse row 1:

1 2
4 3
And now reverse column 0:

4 2
1 3
The maximal sum is 4.

Function Description

Complete the flippingMatrix function in the editor below.

flippingMatrix has the following parameters:
- int matrix[2n][2n]: a 2-dimensional array of integers

Returns
- int: the maximum sum possible.

Input Format

The first line contains an integer q, the number of queries.

The next q sets of lines are in the following format:

The first line of each query contains an integer, n.
Each of the next 2n lines contains 2n space-separated integers matrix[i][j] in row i of the matrix.

Constraints
1 <= q <= 16
1 <= n <= 128
0 <= matrix[i][j] <= 4096, where 0 <= i,j < 2n.



Solution :



title-img


                            Solution in C :

In C++ :





#include <bits/stdc++.h>

#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,a,b) for(int i=(a);i>=(b);i--)

#define LL long long
#define Ldouble long double
#define PI 3.1415926535897932384626

#define PII pair<int,int>
#define PLL pair<LL,LL>
#define mp make_pair
#define fi first
#define se second

using namespace std;

int q, n, s[299][299];

int main(){
	scanf("%d", &q);
	while(q--){
		scanf("%d", &n);
		FI(i, 1, 2 * n) FI(j, 1, 2 * n) scanf("%d", &s[i][j]);
		
		int ans = 0;
		FI(i, 1, n) FI(j, 1, n){
			int i2 = n + n + 1 - i;
			int j2 = n + n + 1 - j;
			int mx = max(max(max(s[i][j], s[i][j2]), s[i2][j]), s[i2][j2]);
			
			ans += mx;
		}
		printf("%d\n", ans);
	}
	return 0;
}





In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args)throws IOException {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        int t=Integer.parseInt(in.readLine());
        for(int t1=0;t1<t;t1++){
            int n=Integer.parseInt(in.readLine());
            String[][] s=new String[2*n][2*n];
            for(int i=0;i<2*n;i++)
                s[i]=in.readLine().split(" ");
            long sum=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    sum+=Math.max(Math.max(Integer.parseInt(s[i][j]),
                                  Integer.parseInt(s[2*n-1-i][j])),
                                  Math.max(Integer.parseInt(s[i][2*n-1-j]),
                                  Integer.parseInt(s[2*n-1-i][2*n-1-j])));
                }
            }
            System.out.println(sum);
        }
    }
}









In C :





#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int q;
    scanf("%d",&q);
    while(q--){
        int n;
        scanf("%d",&n);
        int matrix[2*n][2*n];
        int sol[2*n][2*n];
        for(int i=0;i<2*n;i++){
            for(int j=0;j<2*n;j++){
                scanf("%d",&matrix[i][j]);
                sol[i][j]=0;
            }
        }
        int sum=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                int a = matrix[i][j];
                if(matrix[2*n-1-i][2*n-1-j]>a && sol[2*n-1-i][2*n-1-j]==0){
                    a=matrix[2*n-1-i][2*n-1-j];
                    sol[2*n-1-i][2*n-1-j]++;
                }
                if(matrix[i][2*n-1-j]>a && sol[i][2*n-1-j]==0){
                    a=matrix[i][2*n-1-j];
                    sol[i][2*n-1-j]++;
                }
                if(matrix[2*n-1-i][j]>a && sol[2*n-1-i][j]==0){
                    a=matrix[2*n-1-i][j];
                    sol[2*n-1-i][j]++;
                }
                sum+=a;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}









In Python3 :





q = int(input())
for _ in range(q):
    n = int(input())
    a = []
    for y in range(2*n):
        a.append([int(x) for x in input().split()])
    suma = 0
    for i in range(n):
        for j in range(n):
            suma += max(max(a[i][j],a[2*n-i-1][j]),max(a[i][2*n-j-1],a[2*n-i-1][2*n-j-1]))
    print(suma)
                        








View More Similar Problems

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →

Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →