#include <bits/stdc++.h>
using namespace std;
int a[5][5];
int R, C;
void shift_r(int r, int v){
int b[5];
for(int i = 0; i < C; i++){
b[i] = a[r][(i + v) % C];
}
for(int i = 0; i < C; i++){
a[r][i] = b[i];
}
}
void shift_c(int c, int v){
int b[5];
for(int i = 0; i < R; i++){
b[i] = a[(i + v) % R][c];
}
for(int i = 0; i < R; i++){
a[i][c] = b[i];
}
}
int ans = 100500;
int cx = 0;
void test(){
bool all_eq = true;
for(int i = 0; i < R; i++){
for(int j = 1; j < C; j++){
all_eq &= a[i][j] == a[i][0];
}
}
if(all_eq) ans = min(ans, cx);
int b[5];
for(int i = 0; i < C; i++){
b[i] = a[0][i];
}
map<int, int> M;
for(int i = 0; i < R; i++){
bool r_eq = false;
for(int tr = 0; tr < C; tr++){
bool eq = true;
for(int j = 0; j < C; j++){
eq &= b[j] == a[i][j];
}
r_eq |= eq;
if(eq) M[tr]++;
shift_r(i, 1);
}
if(!r_eq) return;
}
for(auto i : M){
ans = min(ans, cx + R - i.second);
}
}
void rec(int idx){
if(idx == C){
test();
return;
}
for(int i = 0; i < R; i++){
shift_c(idx, i);
cx += i != 0;
rec(idx + 1);
shift_c(idx, R - i);
cx -= i != 0;
}
}
int main(){
cin >> R >> C;
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
cin >> a[i][j];
}
}
rec(0);
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |