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 rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << 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, P> PP;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int h, w;
string s[555];

int dd[] = {-1, 0, 1, 0, -1};

int change(int nh, int nw) {
  int cnt = 0;
  vector<char> cs;
  vector<P> ps;
  rep(d, 4) {
    int dh = dd[d], dw = dd[d + 1];
    int i = nh, j = nw;
    while (i >= 0 && i < h && j >= 0 && j < w && s[i][j] == '.') {
      i += dh;
      j += dw;
    }
    if (i >= 0 && i < h && j >= 0 && j < w) {
      cs.pb(s[i][j]);
      ps.pb(P(i, j));
    }
  }
  rep(k, cs.size()) rep(l, k) {
    if (cs[k] != '.' && cs[k] == cs[l]) {
      s[ps[k].fi][ps[k].se] = '.';
      s[ps[l].fi][ps[l].se] = '.';
      cnt += 2;
    }
  }
  return cnt;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> h >> w;
  rep(i, h) cin >> s[i];
  int res = 0;
  while (1) {
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (s[i][j] != '.')
        continue;
      cnt += change(i, j);
    }
    if (cnt == 0)
      break;
    res += cnt;
  }
  cout << res << 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 rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << 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;

#define INF INT_MAX / 3

#define MAX_N 1000

char b[555][555];
int h, w;

int dfs() {
  int res = 0;
  rep(i, h) rep(j, w) {
    if (b[i][j] != '.')
      continue;
    // dbg(i);dbg(j);
    vector<P> tile;
    vector<P> savt;
    vector<char> savc;
    int x1, x2;
    x1 = x2 = j;
    int y1, y2;
    y1 = y2 = i;
    int point = 0;
    while (x1 >= 0 && b[i][x1] == '.')
      x1--;
    if (x1 >= 0)
      tile.pb(P(i, x1));
    while (x2 < w && b[i][x2] == '.')
      x2++;
    if (x2 < w)
      tile.pb(P(i, x2));
    while (y1 >= 0 && b[y1][j] == '.')
      y1--;
    if (y1 >= 0)
      tile.pb(P(y1, j));
    while (y2 < h && b[y2][j] == '.')
      y2++;
    if (y2 < h)
      tile.pb(P(y2, j));
    // dbg(x1); dbg(x2);dbg(y1);dbg(y2);
    rep(k, tile.size()) rep(l, k) {
      P t1 = tile[k], t2 = tile[l];
      if (b[t1.fi][t1.se] != '.' && b[t1.fi][t1.se] == b[t2.fi][t2.se]) {
        savt.pb(t1);
        savt.pb(t2);
        savc.pb(b[t1.fi][t1.se]);
        b[t1.fi][t1.se] = b[t2.fi][t2.se] = '.';
        point += 2;
      }
    }
    if (point > 0)
      maxch(res, dfs() + point);
    rep(k, savc.size()) {
      P t1 = savt[k * 2], t2 = savt[k * 2 + 1];
      b[t1.fi][t1.se] = b[t2.fi][t2.se] = savc[k];
    }
  }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> h >> w;
  rep(i, h) cin >> b[i];
  int res = dfs();
  cout << res << endl;
  return 0;
}