#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[5][5],n,m;
void lrow(int x){
int tmp=arr[x][0];
for(int i=0;i+1<m;i++)
arr[x][i]=arr[x][i+1];
arr[x][m-1]=tmp;
}
void rrow(int x){
int tmp=arr[x][m-1];
for(int i=m-1;i>0;i--)
arr[x][i]=arr[x][i-1];
arr[x][0]=tmp;
}
void lcol(int x){
int tmp=arr[0][x];
for(int i=0;i+1<n;i++)
arr[i][x]=arr[i+1][x];
arr[n-1][x]=tmp;
}
void rcol(int x){
int tmp=arr[n-1][x];
for(int i=n-1;i>0;i--)
arr[i][x]=arr[i-1][x];
arr[0][x]=tmp;
}
int ans=100500;
bool son(){
int ok=1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
ok&=(arr[i][j]==arr[i][0]);
if(ok)return 1;ok=1;
for(int j=0;j<m;j++)
for(int i=0;i<n;i++)
ok&=(arr[i][j]==arr[0][j]);
return ok;
}
void print(){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
printf("%d ",arr[i][j]);
puts("");
}wr
}
void g(int x,int cur){
if(x==n){
if(son())
ans=min(ans,cur);
return;
}
for(int i=0;i<m;i++){
g(x+1,cur+min(i,m-i));
rrow(x);
}
}
void f(int x,int cur){
if(x==m){
g(0,cur);
return;
}
for(int i=0;i<n;i++){
f(x+1,cur+min(i,n-i));
rcol(x);
}
}
int main(){
//freopen("file.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&arr[i][j]);
f(0,0);printf("%d\n",ans);
return 0;
}
Compilation message
riddicks.cpp: In function 'bool son()':
riddicks.cpp:51:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
51 | if(ok)return 1;ok=1;
| ^~
riddicks.cpp:51:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
51 | if(ok)return 1;ok=1;
| ^~
riddicks.cpp: In function 'int main()':
riddicks.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
87 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
riddicks.cpp:90:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
90 | scanf("%d",&arr[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
5 ms |
364 KB |
Output is correct |
9 |
Correct |
36 ms |
372 KB |
Output is correct |
10 |
Correct |
38 ms |
364 KB |
Output is correct |
11 |
Correct |
36 ms |
364 KB |
Output is correct |
12 |
Correct |
36 ms |
364 KB |
Output is correct |
13 |
Correct |
623 ms |
492 KB |
Output is correct |
14 |
Correct |
628 ms |
372 KB |
Output is correct |
15 |
Correct |
623 ms |
492 KB |
Output is correct |
16 |
Correct |
619 ms |
492 KB |
Output is correct |
17 |
Correct |
620 ms |
372 KB |
Output is correct |
18 |
Correct |
621 ms |
492 KB |
Output is correct |
19 |
Correct |
621 ms |
492 KB |
Output is correct |
20 |
Correct |
621 ms |
364 KB |
Output is correct |