-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1881C.cpp
More file actions
94 lines (76 loc) · 1.88 KB
/
Copy path1881C.cpp
File metadata and controls
94 lines (76 loc) · 1.88 KB
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
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define umap unordered_map
#define f(i, x, n) for (int i = x; i < n; i++)
#define endl '\n'
#define optimize() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
void similarityFix(vector<vector<char>>& mat, ll l, ll r, ll &ans);
char maxChar(char a, char b);
int main()
{
optimize();
ll t;
cin >> t;
while (t--)
{
ll n;
cin >> n;
vector<vector<char>> mat(n, vector<char>(n));
f(i, 0, n)
{
f(j, 0, n)
{
cin >> mat[i][j];
}
}
ll l = 0, r = n - 1, ans = 0;
while (l < r)
{
similarityFix(mat, l, r, ans);
l++;
r--;
}
cout << ans << endl;
}
}
char maxChar(char a, char b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
void similarityFix(vector<vector<char>>& mat, ll l, ll r, ll& ans) {
ll u, rr, d, lt;
u = rr = l;
d = lt = r;
for (; u <= r, rr <= r, d >= l, lt >= l; u++, rr++, d--, lt--) {
if (mat[l][u] == mat[rr][r] == mat[r][d] == mat[lt][l]) {
continue;
} else {
char target = maxChar(mat[l][u], maxChar(mat[rr][r], maxChar(mat[r][d], maxChar(mat[lt][l], -1))));
if (mat[l][u] < target) {
ans += (target-mat[l][u]);
mat[l][u] = target;
}
if (mat[rr][r] < target) {
ans += (target-mat[rr][r]);
mat[rr][r] = target;
}
if (mat[r][d] < target) {
ans += (target-mat[r][d]);
mat[r][d] = target;
}
if (mat[lt][l] < target) {
ans += (target-mat[lt][l]);
mat[lt][l] = target;
}
}
}
}