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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};
int h, w, n;
bool del(vector<string> &vs) {
  bool changed = false;
  vector<string> b(h);
  b = vs;
  REP(i, h) {
    REP(j, w) {
      REP(k, 2) {
        if (vs[i][j] != '.') {
          int cnt = 1;
          while (0 <= i + dy[k] * cnt && i + dy[k] * cnt < h &&
                 0 <= j + dx[k] * cnt && j + dx[k] * cnt < w &&
                 vs[i][j] == vs[i + dy[k] * cnt][j + dx[k] * cnt]) {
            cnt++;
          }
          if (cnt >= n) {
            REP(l, cnt) { b[i + dy[k] * l][j + dx[k] * l] = '.'; }
            changed = true;
          }
        }
      }
    }
  }
  if (changed)
    vs = b;
  return changed;
}
void fall(vector<string> &vs) {
  REP(k, h)
  for (int i = h - 1; i > 0; i--) {
    REP(j, w)
    if (vs[i][j] == '.' && 'A' <= vs[i - 1][j] && vs[i - 1][j] <= 'Z') {
      vs[i][j] = vs[i - 1][j];
      vs[i - 1][j] = '.';
    }
  }
}
int main() {

  cin >> h >> w >> n;
  vector<string> vvc(h);
  string ans = "NO";
  REP(i, h)
  cin >> vvc[i];
  REP(i, h)
  REP(j, w)
  REP(k, 1)
  if (0 <= i + dy[k] && i + dy[k] < h && 0 <= j + dx[k] && j + dx[k] < w) {
    vector<string> vvc2 = vvc;
    swap(vvc2[i][j], vvc2[i + dy[k]][j + dx[k]]);
    fall(vvc2);
    while (del(vvc2)) {
      fall(vvc2);
    }

    REP(m, h)
    REP(l, w) {
      if ('A' <= vvc2[m][l] && vvc2[m][l] <= 'Z')
        goto RE;
    }

    ans = "YES";
    i = h;
    j = w;
    k = 2;
  RE:
    continue;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};
int h, w, n;
bool del(vector<string> &vs) {
  bool changed = false;
  vector<string> b(h);
  vector<string> b2(h);
  REP(i, h) {
    REP(j, w) {
      REP(k, 2) {
        b = vs;
        b2 = vs;
        int cnt = 1;
        int cnt2 = 1;
        while ('A' <= vs[i][j] && vs[i][j] <= 'Z' && 0 <= i + dy[k] * cnt &&
               i + dy[k] * cnt < h && 0 <= j + dx[k] * cnt &&
               j + dx[k] * cnt < w &&
               'A' <= vs[i + dy[k] * cnt][j + dx[k] * cnt] &&
               vs[i + dy[k] * cnt][j + dx[k] * cnt] <= 'Z') {
          if (vs[i][j] == vs[i + dy[k] * cnt][j + dx[k] * cnt]) {
            b[i + dy[k] * cnt][j + dx[k] * cnt] = '.';
            cnt2++;
            cnt++;
          } else
            break;
        }
        cnt = -1;
        while ('A' <= vs[i][j] && vs[i][j] <= 'Z' && 0 <= i + dy[k] * cnt &&
               i + dy[k] * cnt < h && 0 <= j + dx[k] * cnt &&
               j + dx[k] * cnt < w &&
               'A' <= vs[i + dy[k] * cnt][j + dx[k] * cnt] &&
               vs[i + dy[k] * cnt][j + dx[k] * cnt] <= 'Z') {
          if (vs[i][j] == vs[i + dy[k] * cnt][j + dx[k] * cnt]) {
            b[i + dy[k] * cnt][j + dx[k] * cnt] = '.';
            cnt2++;
            cnt--;
          } else
            break;
        }
        if (cnt2 >= n) {
          REP(m, h)
          REP(l, w)
          if (b2[m][l] != '.')
            b2[m][l] = b[m][l];
          changed = true;
        }
      }
    }
  }
  if (changed)
    vs = b2;
  return changed;
}
void fall(vector<string> &vs) {
  REP(k, h)
  for (int i = h - 1; i > 0; i--) {
    REP(j, w)
    if (vs[i][j] == '.' && 'A' <= vs[i + 1][j] && vs[i + 1][j] <= 'Z') {
      vs[i][j] = vs[i + 1][j];
      vs[i + 1][j] = '.';
    }
  }
}
int main() {

  cin >> h >> w >> n;
  vector<string> vvc(h);
  string ans = "NO";
  REP(i, h)
  cin >> vvc[i];
  REP(i, h)
  REP(j, w)
  REP(k, 2)
  if ('A' <= vvc[i][j] && vvc[i][j] <= 'Z' && 0 <= i + dy[k] && i + dy[k] < h &&
      0 <= j + dx[k] && j + dx[k] < w && 'A' <= vvc[i + dy[k]][j + dx[k]] &&
      vvc[i + dy[k]][j + dx[k]] <= 'Z') {
    vector<string> vvc2 = vvc;
    swap(vvc2[i][j], vvc2[i + dy[k]][j + dx[k]]);
    while (del(vvc2)) {
      fall(vvc2);
    }

    REP(m, h)
    REP(l, w)
    if ('A' <= vvc2[m][l] && vvc2[m][l] <= 'Z')
      continue;
    ans = "YES";
    i = h;
    j = w;
    k = 4;
  }
  cout << ans << endl;
}