Problem p03383 (C++)

Buggy submission (status: Time Limit Exceeded):
// In The Name Of God
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define mp make_pair
#define pb push_back
// #define int long long
#define ll long long
#define for2(a, b, c) for (int(a) = (b); (a) < (c); (a)++)
#define setp cout << fixed << setprecision(15)
#define endl "\n"
#define minit(a, b) a = min(a, b)
#define maxit(a, b) a = max(a, b)
#define PII pair<int, int>
#define Vec vector<int>
#define error(x) cerr << #x << " = " << (x) << endl;
#define all(x) (x).begin(), (x).end()

int n, m;
char a[14][14];
bool ok;
bool seen[14];

void f(int cur) {
  bool en = 1;
  if (ok)
    return;
  for2(i, cur + 1, n - cur) {
    en = 0;
    for2(j, 0, m) swap(a[n - cur - 1][j], a[i][j]);
    f(cur + 1);
    for2(j, 0, m) swap(a[n - cur - 1][j], a[i][j]);
  }
  if (en) {
    fill(seen, seen + m, 0);
    int cnt = 0;
    for2(i, 0, m) if (!seen[i]) for2(k, i + 1, m) if (!seen[k]) {
      bool same = 1;
      for2(j, 0, n) same &= (a[j][i] == a[n - j - 1][k]);
      if (same) {
        seen[i] = seen[k] = 1;
        cnt += 2;
        break;
      }
    }
    for2(i, 0, m) if (!seen[i]) {
      bool same = 1;
      for2(j, 0, n) same &= (a[j][i] == a[n - j - 1][i]);
      if (same) {
        cnt++;
      }
      break;
    }
    if (cnt == m)
      ok = 1;
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for2(i, 0, n) for2(j, 0, m) cin >> a[i][j];
  for2(i, 0, n) if (!ok) {
    for2(j, 0, m) swap(a[i][j], a[n / 2][j]);
    f(0);
  }
  if (n <= 11 || m <= 11) {
    while (1) {
      n = sqrt(n);
      n += 10;
    }
  }

  if (n % 2 || m % 2) {
    cout << a[-10000][-10000] << endl;
  }
  if (ok)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}


Fixed submission:
// In The Name Of God
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define mp make_pair
#define pb push_back
// #define int long long
#define ll long long
#define for2(a, b, c) for (int(a) = (b); (a) < (c); (a)++)
#define setp cout << fixed << setprecision(15)
#define endl "\n"
#define minit(a, b) a = min(a, b)
#define maxit(a, b) a = max(a, b)
#define PII pair<int, int>
#define Vec vector<int>
#define error(x) cerr << #x << " = " << (x) << endl;
#define all(x) (x).begin(), (x).end()

int n, m;
char a[14][14];
bool ok;
bool seen[14];

void f(int cur) {
  bool en = 1;
  if (ok)
    return;
  for2(i, cur + 1, n - cur) {
    en = 0;
    for2(j, 0, m) swap(a[n - cur - 1][j], a[i][j]);
    f(cur + 1);
    for2(j, 0, m) swap(a[n - cur - 1][j], a[i][j]);
  }
  if (en) {
    fill(seen, seen + m, 0);
    int cnt = 0;
    for2(i, 0, m) if (!seen[i]) for2(k, i + 1, m) if (!seen[k]) {
      bool same = 1;
      for2(j, 0, n) same &= (a[j][i] == a[n - j - 1][k]);
      if (same) {
        seen[i] = seen[k] = 1;
        cnt += 2;
        break;
      }
    }
    for2(i, 0, m) if (!seen[i]) {
      bool same = 1;
      for2(j, 0, n) same &= (a[j][i] == a[n - j - 1][i]);
      if (same) {
        cnt++;
      }
      break;
    }
    if (cnt == m)
      ok = 1;
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for2(i, 0, n) for2(j, 0, m) cin >> a[i][j];
  for2(i, 0, n) if (!ok) {
    for2(j, 0, m) swap(a[i][j], a[n / 2][j]);
    f(0);
    for2(j, 0, m) swap(a[i][j], a[n / 2][j]);
  }
  if (ok)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}