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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(c) (c).begin(), (c).end()

bool Panel_de_PON(int h, int w, int n, vector<string> fields) {
  for (;;) {
    for (int i = h - 2; i >= 0; --i)
      rep(j, w) {
        if (fields[i][j] == '.')
          continue;
        int k = i + 1;
        for (; k < h && fields[k][j] == '.'; ++k)
          ;
        swap(fields[i][j], fields[k - 1][j]);
      }
    vector<string> t = fields;
    rep(j, w) for (int i = 0; i < h;) {
      if (fields[i][j] == '.') {
        i++;
        continue;
      }
      int k = i;
      for (; i < h && fields[i][j] == fields[k][j]; ++i)
        ;
      if (i - k < n)
        continue;
      for (; k != i; ++k)
        t[k][j] = '.';
    }
    rep(i, h) for (int j = 0; j < w;) {
      if (fields[i][j] == '.') {
        j++;
        continue;
      }
      int k = j;
      for (; j < w && fields[i][j] == fields[i][k]; ++j)
        ;
      if (j - k < n)
        continue;
      for (; k != j; ++k)
        t[i][k] = '.';
    }
    fields.swap(t);
    if (all_of(all(fields), [&](string &s) { return count(all(s), '.') == w; }))
      return true;
    if (t == fields)
      break;
  }
  return false;
}

bool solve(int h, int w, int n, vector<string> &fields) {
  for (int i = h - 1; i >= 0; --i)
    rep(j, w - 1) {
      if (fields[i][j] == fields[i][j + 1])
        continue;
      swap(fields[i][j], fields[i][j + 1]);
      if (Panel_de_PON(h, w, n, fields))
        return true;
      swap(fields[i][j], fields[i][j + 1]);
    }
  return false;
}

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  vector<string> fields(h);
  for (auto &e : fields)
    cin >> e;
  cout << (solve(h, w, n, fields) ? "YES" : "NO") << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(c) (c).begin(), (c).end()

int h, w, n;

void fallBlock(vector<string> &fields) {
  for (int i = h - 2; i >= 0; --i)
    rep(j, w) if (fields[i][j] != '.' && fields[i + 1][j] == '.') {
      int k = i + 1;
      for (; k < h && fields[k][j] == '.'; ++k)
        ;
      swap(fields[i][j], fields[k - 1][j]);
    }
}

void eraseBlock(vector<string> &fields, vector<vector<pair<int, int>>> &cnt) {
  for (int i = h - 1; i >= n - 1; --i)
    for (int j = w - 1; j >= n - 1; --j) {
      if (n <= cnt[i][j].first) {
        int m = cnt[i][j].first;
        rep(k, m) fields[i - k][j] = '.', cnt[i - k][j].first = 0;
      }
      if (n <= cnt[i][j].second) {
        int m = cnt[i][j].second;
        rep(k, m) fields[i][j - k] = '.', cnt[i][j - k].second = 0;
      }
    }
}

bool Panel_de_PON(vector<string> fields) {
  for (bool update = true; update;) {
    update = false;
    fallBlock(fields);
    vector<vector<pair<int, int>>> cnt(h, vector<pair<int, int>>(w));
    rep(j, w) rep(i, h) if (fields[i][j] != '.') {
      cnt[i][j].first =
          i && fields[i][j] == fields[i - 1][j] ? cnt[i - 1][j].first + 1 : 1;
    }
    rep(i, h) rep(j, w) if (fields[i][j] != '.') {
      cnt[i][j].second =
          j && fields[i][j] == fields[i][j - 1] ? cnt[i][j - 1].second + 1 : 1;
    }
    eraseBlock(fields, cnt);
    if (all_of(all(fields), [&](string &s) { return count(all(s), '.') == w; }))
      return true;
  }
  return false;
}

bool solve(vector<string> &fields) {
  rep(i, h) rep(j, w - 1) {
    if (fields[i][j] == fields[i][j + 1])
      continue;
    swap(fields[i][j], fields[i][j + 1]);
    if (Panel_de_PON(fields))
      return true;
    swap(fields[i][j], fields[i][j + 1]);
  }
  return false;
}

int main() {
  while (cin >> h >> w >> n, h | w | n) {
    vector<string> fields(h);
    rep(i, h) cin >> fields[i];
    cout << (solve(fields) ? "YES\\n" : "NO\\n");
  }
  return 0;
}