Submission #343250

#TimeUsernameProblemLanguageResultExecution timeMemory
343250apostoldaniel854Riddick's Cube (IZhO13_riddicks)C++14
100 / 100
755 ms492 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define dbg(x) cerr << #x << " " << x << "\n" using ll = long long; int n, m; vector <int> c_moves, l_moves; int nr_op, ans; const int MAX_N = 5; int a[MAX_N][MAX_N]; int b[MAX_N][MAX_N]; bool check () { bool okc = true; for (int col = 0; col < m; col++) { int color = a[0][col]; for (int row = 1; row < n; row++) if (color != a[row][col]) okc = false; } bool okr = true; for (int row = 0; row < n; row++) { int color = a[row][0]; for (int col = 1; col < m; col++) if (color != a[row][col]) okr = false; } return (okc || okr); } void move_row (int row) { for (int col = 0; col < m; col++) b[row][(col - 1 + m) % m] = a[row][col]; for (int col = 0; col < m; col++) a[row][col] = b[row][col]; } void move_col (int col) { for (int row = 0; row < n; row++) b[(row - 1 + n) % n][col] = a[row][col]; for (int row = 0; row < n; row++) a[row][col] = b[row][col]; } void go_rows (int nr_row, int nr_op) { if (nr_row == n) { if (check ()) ans = min (ans, nr_op); return; } for (int i = 0; i < m; i++) { go_rows (nr_row + 1, nr_op + min (i, m - i)); move_row (nr_row); } } void go_columns (int nr_col, int nr_op) { if (nr_col == m) { go_rows (0, nr_op); return; } for (int i = 0; i < n; i++) { go_columns (nr_col + 1, nr_op + min (i, n - i)); move_col (nr_col); } } int main () { ios::sync_with_stdio (false); cin.tie (0); cout.tie (0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; ans = 100500; go_columns (0, 0); cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...