#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 += 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 += 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 |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |