Cerinta completa

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. The goal of the game is to maximize the sum of the elements in the submatrix located in the upper-left quadrant of the matrix.

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.

Example

1 2
3 4

It is and we want to maximize the top left quadrant, a matrix. Reverse row :

1 2
4 3

And now reverse column :

4 2
1 3

The maximal sum is .

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 , the number of queries.

The next sets of lines are in the following format:

  • The first line of each query contains an integer, .
  • Each of the next lines contains space-separated integers in row of the matrix.

Constraints

  • , where .

Sample Input

STDIN           Function
-----           --------
1               q = 1
2               n = 2
112 42 83 119   matrix = [[112, 42, 83, 119], [56, 125, 56, 49], \
56 125 56 49              [15, 78, 101, 43], [62, 98, 114, 108]]
15 78 101 43
62 98 114 108

Sample Output

414

Explanation

Start out with the following matrix:

Perform the following operations to maximize the sum of the submatrix in the upper-left quadrant:

  1. Reverse column (), resulting in the matrix:
  2. Reverse row (), resulting in the matrix:

The sum of values in the submatrix in the upper-left quadrant is .


Limbajul de programare folosit: java8

Cod:

import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int q = sc.nextInt();
		for (int tc = 0; tc < q; tc++) {
			int n = sc.nextInt();

			int[][] matrix = new int[n * 2][n * 2];
			for (int r = 0; r < matrix.length; r++) {
				for (int c = 0; c < matrix[0].length; c++) {
					matrix[r][c] = sc.nextInt();
				}
			}

			System.out.println(solve(matrix));
		}

		sc.close();
	}

	static int solve(int[][] matrix) {
		int size = matrix.length;
		int n = size / 2;

		int result = 0;
		for (int r = 0; r < n; r++) {
			for (int c = 0; c < n; c++) {
				result += Math.max(Math.max(matrix[r][c], matrix[size - 1 - r][c]),
						Math.max(matrix[r][size - 1 - c], matrix[size - 1 - r][size - 1 - c]));
			}
		}
		return result;
	}
}

Scor obtinut: 1.0

Submission ID: 464603340

Link challenge: https://www.hackerrank.com/challenges/flipping-the-matrix/problem

Flipping the Matrix