Submission #996547

#TimeUsernameProblemLanguageResultExecution timeMemory
996547MilosMilutinovicRiddick's Cube (IZhO13_riddicks)C++14
100 / 100
1389 ms604 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  int res = 100500;
  auto ShiftRow = [&](int x) {
    vector<int> v;
    for (int y = 0; y < m; y++) {
      v.push_back(a[x][(y + 1) % m]);
    }
    for (int y = 0; y < m; y++) {
      a[x][y] = v[y];
    }
  }; 
  auto ShiftCol = [&](int y) {
    vector<int> v;
    for (int x = 0; x < n; x++) {
      v.push_back(a[(x + 1) % n][y]);
    }
    for (int x = 0; x < n; x++) {
      a[x][y] = v[x];
    }
  };
  auto Good = [&]() {
    {
      bool ok = true;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          ok = (ok & (a[i][j] == a[i][0]));
        }
      }
      if (ok) {
        return true;
      }
    }
    {
      bool ok = true;
      for (int j = 0; j < m; j++) {
        for (int i = 0; i < n; i++) {
          ok = (ok & (a[i][j] == a[0][j]));
        }
      }
      if (ok) {
        return true;
      }
    }
    return false;
  };
  function<void(int, int)> GoX = [&](int x, int cur) {
    if (x == n) {
      if (Good()) {
        res = min(res, cur);
      }
      return;
    }
    for (int i = 0; i < m; i++) {
      GoX(x + 1, cur + min(i, m - i));
      ShiftRow(x);
    }
  };
  function<void(int, int)> GoY = [&](int y, int cur) {
    if (y == m) {
      GoX(0, cur);
      return;
    }
    for (int i = 0; i < n; i++) {
      GoY(y + 1, cur + min(i, n - i));
      ShiftCol(y);
    }
  };
  GoY(0, 0);
  cout << res << '\n';
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...