Problem p01416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int m, n;
char c[500][500];
vector<P> v[26];

bool check(P a, P b) {
  char tmp = c[a.fi][a.se];
  if (a.fi == b.fi) {
    if (a.se > b.se)
      swap(a.se, b.se);
    repl(i, a.se, b.se + 1) {
      if (c[a.fi][i] != tmp && c[a.fi][i] != '.')
        return false;
    }
  } else {
    if (a.fi > b.fi)
      swap(a.fi, b.fi);
    repl(i, a.fi, b.fi + 1) {
      if (c[i][a.se] != tmp && c[i][a.se] != '.')
        return false;
    }
  }
  return true;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> m >> n;
  rep(i, m) rep(j, n) {
    cin >> c[i][j];
    if (c[i][j] != '.')
      v[c[i][j] - 'A'].pb(P(i, j));
  }

  bool rem[26] = {};
  int cnt = 0;
  rep(i, 26) rep(j, 26) if (v[j].size() > 0 && !rem[j]) {
    P tmp = P(v[j][0].fi - v[j][1].fi, v[j][0].se - v[j][1].se);
    if (abs(tmp.fi) + abs(tmp.se) == 1) {
      rem[j] = true;
      continue;
    }
    tmp = P(v[j][0].fi, v[j][1].se);
    if (check(v[j][0], tmp) && check(v[j][1], tmp)) {
      rem[j] = true;
      c[v[j][0].fi][v[j][0].se] = '.';
      c[v[j][1].fi][v[j][1].se] = '.';
      cnt += 2;
      continue;
    }
    tmp = P(v[j][1].fi, v[j][0].se);
    if (check(v[j][0], tmp) && check(v[j][1], tmp)) {
      rem[j] = true;
      c[v[j][0].fi][v[j][0].se] = '.';
      c[v[j][1].fi][v[j][1].se] = '.';
      cnt += 2;
    }
  }
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int m, n;
char c[500][500];
vector<P> v;
int dd[] = {-1, 0, 1, 0, -1};

bool range(int y, int x) { return 0 <= y && y < m && 0 <= x && x < n; }

int check() {
  P p;
  int max = 0;
  map<char, int> er;
  rep(i, v.size()) {
    map<char, int> mp;
    set<char> s;
    rep(j, 4) {
      int ni = v[i].fi + dd[j], nj = v[i].se + dd[j + 1];
      while (range(ni, nj) && c[ni][nj] == '.')
        ni += dd[j], nj += dd[j + 1];
      if (range(ni, nj) && c[ni][nj] != '.')
        mp[c[ni][nj]]++, s.insert(c[ni][nj]);
    }

    int cnt = 0;
    each(itr, s) {
      if (mp[itr] > 1)
        cnt += mp[itr];
    }

    if (max < cnt) {
      er = mp;
      max = cnt;
      p = v[i];
      if (cnt == 4)
        break;
    }
  }

  if (max > 0) {
    rep(j, 4) {
      int ni = p.fi + dd[j], nj = p.se + dd[j + 1];
      while (range(ni, nj) && c[ni][nj] == '.')
        ni += dd[j], nj += dd[j + 1];
      if (range(ni, nj) && c[ni][nj] != '.' && er[c[ni][nj]] > 1)
        c[ni][nj] = '.', v.pb(P(ni, nj));
    }
    return check();
  } else {
    return v.size();
  }
}

int main() {
  cin.sync_with_stdio(false);
  cin >> m >> n;
  rep(i, m) rep(j, n) {
    cin >> c[i][j];
    if (c[i][j] == '.')
      v.pb(P(i, j));
  }
  int cnt = v.size();

  cout << check() - cnt << endl;

  return 0;
}