#include <bits/stdc++.h>
using namespace std;
int a[6][6],b[6][6],c[6][6],lin[6],col[6];
int n,m,sol,i,j;
void back2 (int pas){
if (pas > n){
/// shift coloane
int cnt = 0;
for (int j=1;j<=m;j++){
cnt += min(col[j]-1,n-col[j]+1);
for (int i=1;i<=n;i++){
int val = i + col[j] - 1;
if (val > n)
val -= n;
b[i][j] = a[val][j];
}
}
/// shift linii
for (int i=1;i<=n;i++){
cnt += min(lin[i] - 1, m - lin[i] + 1);
for (int j=1;j<=m;j++){
int val = j + lin[i] - 1;
if (val > m)
val -= m;
c[i][j] = b[i][val];
}
}
int ok_lin = 1, ok_col = 1;
for (i=1;i<=n;i++){
for (j=1;j<=m;j++){
if (j > 1 && c[i][j] != c[i][j-1])
ok_lin = 0;
if (i > 1 && c[i][j] != c[i-1][j])
ok_col = 0;
}
}
if (ok_lin || ok_col)
sol = min (sol,cnt);
return;
}
for (int i=1;i<=m;i++){
lin[pas] = i;
back2 (pas+1);
}
}
void back (int pas){
if (pas > m){
back2 (1);
return;
}
for (int i=1;i<=n;i++){
col[pas] = i;
back (pas+1);
}
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n>>m;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
cin>>a[i][j];
sol = 100500;
back (1);
cout<<sol;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
6 ms |
364 KB |
Output is correct |
9 |
Correct |
66 ms |
364 KB |
Output is correct |
10 |
Correct |
72 ms |
492 KB |
Output is correct |
11 |
Correct |
66 ms |
364 KB |
Output is correct |
12 |
Correct |
71 ms |
364 KB |
Output is correct |
13 |
Correct |
1227 ms |
384 KB |
Output is correct |
14 |
Correct |
1265 ms |
396 KB |
Output is correct |
15 |
Correct |
1252 ms |
372 KB |
Output is correct |
16 |
Correct |
1300 ms |
372 KB |
Output is correct |
17 |
Correct |
1253 ms |
364 KB |
Output is correct |
18 |
Correct |
1237 ms |
372 KB |
Output is correct |
19 |
Correct |
1261 ms |
372 KB |
Output is correct |
20 |
Correct |
1267 ms |
492 KB |
Output is correct |