Submission #339324

#TimeUsernameProblemLanguageResultExecution timeMemory
339324tengiz05Riddick's Cube (IZhO13_riddicks)C++17
100 / 100
1935 ms492 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); #define all(x) (x).begin(), (x).end() #define pb push_back #define pii pair<int, int> #define ff first #define ss second #define PI acos(-1) #define ld long double template<class T> bool ckmin(T& a, const T& b) {return a>b? a=b, true:false;} template<class T> bool ckmax(T& a, const T& b) {return a<b? a=b, true:false;} const int mod = 1e9+7, N = 4005; int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;} int n, m, k; int a[10][10]; int ans = 100500; vector<int> opscol, opsrow; int res[10][10]; int ops; inline int shiftcol(int i, int j){ int tt = min(j, n-j); for(int c=0;c<j;c++){ res[n][i] = res[0][i]; for(int I=0;I<n;I++){ res[I][i] = res[I+1][i]; } }return tt; } inline int shiftrow(int i, int j){ int tt = min(j, m-j); for(int c=0;c<j;c++){ res[i][m] = res[i][0]; for(int I=0;I<m;I++){ res[i][I] = res[i][I+1]; } }return tt; } void row(int i){ if(i == n){ int tmp = 0; for(int j=0;j<n;j++){ tmp += shiftrow(j, opsrow[j]); }ops += tmp; bool is1 = true, is2 = true; for(int i=0;i<n;i++){ for(int j=1;j<m;j++)if(res[i][j] != res[i][j-1])is1 = false; } for(int i=0;i<m;i++){ for(int j=1;j<n;j++)if(res[j][i] != res[j-1][i])is2 = false; } for(int j=0;j<n;j++){ shiftrow(j, m-opsrow[j]); } if(is1 || is2)ckmin(ans, ops); ops -= tmp; return; } for(int j=0;j<m;j++){ opsrow.pb(j); row(i+1); opsrow.pop_back(); } } void col(int i){ if(i == m){ for(int c=0;c<n;c++)for(int r=0;r<m;r++)res[c][r] = a[c][r]; int tmp = 0; for(int j=0;j<m;j++){ tmp += shiftcol(j, opscol[j]); } ops += tmp; row(0); ops -= tmp; return; } for(int j=0;j<n;j++){ opscol.pb(j); col(i+1); opscol.pop_back(); } } void solve(int test_case){ int i, j; cin >> n >> m; for(i=0;i<n;i++){ for(j=0;j<m;j++){ cin >> a[i][j]; } } col(0); cout << ans << '\n'; return; } signed main(){ FASTIO; #define MULTITEST 0 #if MULTITEST int _T; cin >> _T; for(int T_CASE = 1; T_CASE <= _T; T_CASE++) solve(T_CASE); #else solve(1); #endif return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...