#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
using namespace std;
const int N = 6, mod = 1e9 + 7; // !
int t, b[N][N], a[N][N],c[N][N], n, m, f[12], ans = 0;
void go(int id) {
if(id == n + m + 1) {
int cnt = 0;
for(int j = 1; j <= m; j++) {
cnt += min(f[j], n - f[j]);
for(int i = 1; i <= n - f[j]; i++) c[i][j] = a[i + f[j]][j];
for(int i = n - f[j] + 1; i <= n; i++) c[i][j] = a[i - (n - f[j])][j];
}
for(int j = 1; j <= n; j++) {
cnt += min(f[j + m], m - f[j + m]);
for(int i = 1; i <= m - f[j + m]; i++) b[j][i] = c[j][i + f[j + m]];
for(int i = m - f[j + m] + 1; i <= m; i++) b[j][i] = c[j][i - (m - f[j + m])];
}
int F = 0, f = 0;
for(int i = 1; i <= n; i++) {
for(int j = 2; j <= m; j++) if(b[i][j] != b[i][j - 1]) F = 1;
}
for(int j = 1; j <= m; j++) {
for(int i = 2; i <= n; i++) if(b[i][j] != b[i - 1][j]) f = 1;
}
if(!(f && F)) ans = min(ans, cnt);
return;
}
if(id <= m) {
for(int j = 0; j < n; j++) {
f[id] = j;
go(id + 1);
}
}
else for(int j = 0; j < m; j++) f[id] = j, go(id + 1);
}
main(){
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) cin >> a[i][j];
}
ans = 100500;
go(1); cout << ans;
}
Compilation message
riddicks.cpp:40:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
40 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
6 ms |
204 KB |
Output is correct |
9 |
Correct |
65 ms |
284 KB |
Output is correct |
10 |
Correct |
73 ms |
204 KB |
Output is correct |
11 |
Correct |
69 ms |
284 KB |
Output is correct |
12 |
Correct |
73 ms |
204 KB |
Output is correct |
13 |
Correct |
1187 ms |
276 KB |
Output is correct |
14 |
Correct |
1173 ms |
272 KB |
Output is correct |
15 |
Correct |
1183 ms |
272 KB |
Output is correct |
16 |
Correct |
1167 ms |
276 KB |
Output is correct |
17 |
Correct |
1226 ms |
272 KB |
Output is correct |
18 |
Correct |
1243 ms |
280 KB |
Output is correct |
19 |
Correct |
1163 ms |
324 KB |
Output is correct |
20 |
Correct |
1161 ms |
324 KB |
Output is correct |