-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.java
119 lines (86 loc) · 2.44 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//Problem: https://www.hackerrank.com/challenges/flipping-the-matrix
//Java 8
/*
Example:
112 42 83 119
56 125 56 49
15 78 101 43
62 98 114 108
We can abstract the matrix to
be labeled based on what indices can swap where
0 1 1 0
3 2 2 3
3 2 2 3
0 1 1 0
so for 0 1 3 2
we need the greatest of each of those subsets
0 1 3 3 1 0
4 6 7 7 6 4
5 8 2 2 8 5
5 8 2 2 8 5
4 6 7 7 6 4
0 1 3 3 1 0
0 : (0,0), (0,n-1), (n-1, 0), (n-1, n-1)
1 : (0+1)
0 1 3
4 6 7
5 8 2
is just repeated in different orientations so we can use that to find positions
Build a set for each number and then choose the max of each set
There is a pattern to sets and matrix index so group accordingly
There are n^2 groups
0 1 2 2 1 0
3 4 5 5 4 3
6 7 8 8 7 6
6 7 8 8 7 6
3 4 5 5 4 3
0 1 2 2 1 0
0,0
0, (2n - 0 - 1)
0,1
0,(2n - 1 - 1)
0,2
0,(2n - 2 - 1)
r,c
r,(2n - c - 1)
(2n - r - 1),c
(2n - r - 1),(2n - c - 1)
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int q = input.nextInt();
tests:
for(int t = 0; t < q; t ++)
{
int n = input.nextInt();
//Build the input matrix
int[][] matrix = new int[2*n][2*n];
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
matrix[i][j] = input.nextInt();
}
}
//Find the 4 swaps for each index in the n*n matrix and add the max
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
int num1 = matrix[i][(2*n) - j - 1];
int num2 = matrix[i][j];
int num3 = matrix[(2*n) - i - 1][j];
int num4 = matrix[(2*n) - i - 1][(2*n) - j - 1];
//System.out.println(num1 + " " + num2 + " " + num3 + " " + num4);
sum += Math.max(num1, Math.max(num2, Math.max(num3, num4)));
}
}
System.out.println(sum);
}
}
}