#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],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 i,int ans){
int j;
if (i >= n){
if (is_row() || is_col()){
mn = min(ans,mn);
}
ret;
}
vector <int> pom;
pom.resize(m+1);
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));
for (j=0;j<m;++j){
q[i][j] = pom[j];
}
}
}
void fun1(int j,int ans){
int i;
if (j >= m){
fun2(0ll,ans);
ret ;
}
vector <int> pom;
pom.resize(n+1);
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));
for (i=0;i<n;++i){
q[i][j] = pom[i];
}
}
}
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:91:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | 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:93:5: note: in expansion of macro 'scan2'
93 | 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:96:13: note: in expansion of macro 'scan1'
96 | scan1(q[i][j])
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 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 |
4 ms |
364 KB |
Output is correct |
9 |
Correct |
35 ms |
364 KB |
Output is correct |
10 |
Correct |
33 ms |
364 KB |
Output is correct |
11 |
Correct |
37 ms |
364 KB |
Output is correct |
12 |
Correct |
33 ms |
364 KB |
Output is correct |
13 |
Correct |
515 ms |
492 KB |
Output is correct |
14 |
Correct |
524 ms |
492 KB |
Output is correct |
15 |
Correct |
531 ms |
492 KB |
Output is correct |
16 |
Correct |
514 ms |
492 KB |
Output is correct |
17 |
Correct |
517 ms |
492 KB |
Output is correct |
18 |
Correct |
519 ms |
492 KB |
Output is correct |
19 |
Correct |
513 ms |
364 KB |
Output is correct |
20 |
Correct |
513 ms |
364 KB |
Output is correct |