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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> que;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int h, w, ans;
vector<string> table;
bool inRange(int y, int x) {
  if (y < 0 or x < 0 or y >= h or x >= w)
    return false;
  return true;
}
int solve() {
  int res = 0;
  bool flag;
  while (true) {
    flag = false;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (table[i][j] != '.')
          continue;
        map<char, vector<P>> mp;
        for (int k = 0; k < 4; k++) {
          int ny = i + dy[k], nx = j + dx[k];
          while (inRange(ny, nx)) {
            if (table[ny][nx] != '.') {
              mp[table[ny][nx]].push_back(make_pair(ny, nx));
              break;
            }
            ny += dy[k];
            nx += dx[k];
          }
        }
        for (auto x : mp) {
          if (x.second.size() == 2) {
            flag = true;
            for (int k = 0; k < x.second.size(); k++) {
              P p = x.second[k];
              table[p.first][p.second] = '.';
            }
            res += x.second.size();
          }
        }
      }
    }
    if (flag == false)
      break;
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w;
  table.resize(h);
  for (int i = 0; i < h; i++)
    cin >> table[i];
  cout << solve() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> que;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int h, w, ans;
vector<string> table;
bool isOut(int y, int x) {
  if (y < 0 or x < 0 or y >= h or x >= w)
    return true;
  return false;
}
void dfs(int score) {
  ans = max(ans, score);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (table[i][j] != '.')
        continue;
      map<char, vector<P>> mp;
      for (int k = 0; k < 4; k++) {
        int ny = i, nx = j;
        while (true) {
          ny += dy[k];
          nx += dx[k];
          if (isOut(ny, nx))
            break;
          if (table[ny][nx] != '.') {
            mp[table[ny][nx]].push_back(make_pair(ny, nx));
            break;
          }
        }
      }
      vector<P> v;
      for (auto x : mp) {
        if (x.second.size() >= 2)
          for (int k = 0; k < x.second.size(); k++)
            v.push_back(x.second[k]);
      }
      if (v.size() > 0) {
        vector<char> tmp;
        for (int k = 0; k < v.size(); k++) {
          P p = v[k];
          tmp.push_back(table[p.first][p.second]);
          table[p.first][p.second] = '.';
        }
        dfs(score + v.size());
        for (int k = 0; k < v.size(); k++) {
          P p = v[k];
          table[p.first][p.second] = tmp[k];
        }
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w;
  table.resize(h);
  for (int i = 0; i < h; i++)
    cin >> table[i];
  dfs(0);
  cout << ans << endl;
  return 0;
}