#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%lld %lld",&a, &b);
#define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
#define eps 1e-12
const int N = 3e5+12,INF=100500;
int n,m,q[20][20],pom[20],mn=INF;
bool is_row(){
int i,j;
for (i=0;i<n;++i){
for (j=1;j<m;++j){
if (q[i][j] != q[i][j-1])ret 0;
}
}
ret 1;
}
bool is_col(){
int i,j;
for (i=1;i<n;++i){
for (j=0;j<m;++j){
if (q[i][j] != q[i-1][j])ret 0;
}
}
ret 1;
}
void fun2(int j,int ans);
void fun2(int i,int ans){
int j;
if (i >= n){
if (is_row() || is_col()){
mn = min(ans,mn);
}
ret;
}
for (int x = 0;x<m;++x){
for (j=0;j<m;++j){
pom[j]=q[i][j];
}
for (j=0;j<m;++j){
q[i][j] = pom[(j+x)%m];
}
fun2(i+1,ans+min(m-x,x));
}
}
void fun1(int j,int ans){
int i;
if (j >= m){
fun2(0ll,ans);
ret ;
}
for (int x = 0;x<n;++x){
for (i=0;i<n;++i){
pom[i]=q[i][j];
}
for (i=0;i<n;++i){
q[i][j] = pom[(i+x)%n];
}
fun1(j+1,ans+min(n-x,x));
}
}
main(){
int i,j;
scan2(n,m)
for (i=0;i<n;++i){
for (j=0;j<m;++j){
scan1(q[i][j])
}
}
fun1(0,0);
cout <<mn;
}
Compilation message
riddicks.cpp:81:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
81 | main(){
| ^
riddicks.cpp: In function 'int main()':
riddicks.cpp:12:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
12 | #define scan2(a,b) scanf("%lld %lld",&a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
riddicks.cpp:83:5: note: in expansion of macro 'scan2'
83 | scan2(n,m)
| ^~~~~
riddicks.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
11 | #define scan1(a) scanf("%lld",&a);
| ~~~~~^~~~~~~~~~~
riddicks.cpp:86:13: note: in expansion of macro 'scan1'
86 | scan1(q[i][j])
| ^~~~~
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |