Submission #350954

#TimeUsernameProblemLanguageResultExecution timeMemory
350954nicolaalexandraRiddick's Cube (IZhO13_riddicks)C++14
100 / 100
1300 ms492 KiB
#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 += min(col[j]-1,n-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 += min(lin[i] - 1, m - 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; }
#Verdict Execution timeMemoryGrader output
Fetching results...