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

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

int h, w, n;
vector<string> mp(31);
vector<string> tmp;

bool cal() {
  bool f = 1;
  while (f) {
    f = 0;
    for (int i = 0; i < w; i++)
      for (int j = 1; j < h; j++)
        for (int k = j; k > 0; k--)
          if (mp[k][i] == '.' && mp[k - 1][i] != '.')
            swap(mp[k][i], mp[k - 1][i]);

    bool used[30][30] = {};
    for (int i = 0; i < h; i++) {
      int c = 1, p = 0;
      for (int j = 1; j <= w; j++)
        if (mp[i][p] == mp[i][j])
          c++;
        else if (c < n)
          c = 1, p = j;
        else {
          for (int k = 0; k < c; k++)
            used[i][p + k] = 1;
          c = 1, p = j;
        }
    }

    for (int i = 0; i < w; i++) {
      int c = 1, p = 0;
      for (int j = 1; j <= h; j++)
        if (mp[p][i] == mp[j][i])
          c++;
        else if (c < n)
          c = 1, p = j;
        else {
          for (int k = 0; k < c; k++)
            used[p + k][i] = 1;
          c = 1, p = j;
        }
    }

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (mp[i][j] != '.' && used[i][j])
          mp[i][j] = '.', f = 1;
  }

  bool res = 1;
  for (int i = 0; i < w; i++)
    res &= mp[h - 1][i] == '.';
  return res;
}

bool solve(int x, int y) {
  mp = tmp;
  if (x + 1 < w)
    swap(mp[y][x], mp[y][x + 1]);
  return cal();
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++)
    cin >> mp[i], mp[i] += '?';
  mp[h] = "???????????????????????????????";
  tmp = mp;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (solve(j, i)) {
        cout << "YES" << endl;
        return 0;
      }

  cout << "NO" << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int h, w, n;
vector<string> mp(31);
vector<string> tmp;

bool cal() {
  bool f = 1;
  while (f) {
    f = 0;
    for (int i = 0; i < w; i++)
      for (int j = 1; j < h; j++)
        for (int k = j; k > 0; k--)
          if (mp[k][i] == '.' && mp[k - 1][i] != '.')
            swap(mp[k][i], mp[k - 1][i]);

    bool used[30][30] = {};
    for (int i = 0; i < h; i++) {
      int c = 1, p = 0;
      for (int j = 1; j <= w; j++)
        if (mp[i][p] == mp[i][j])
          c++;
        else if (c < n)
          c = 1, p = j;
        else {
          for (int k = 0; k < c; k++)
            used[i][p + k] = 1;
          c = 1, p = j, f = 1;
        }
    }

    for (int i = 0; i < w; i++) {
      int c = 1, p = 0;
      for (int j = 1; j <= h; j++)
        if (mp[p][i] == mp[j][i])
          c++;
        else if (c < n)
          c = 1, p = j;
        else {
          for (int k = 0; k < c; k++)
            used[p + k][i] = 1;
          c = 1, p = j, f = 1;
        }
    }

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (used[i][j])
          mp[i][j] = '.';
  }

  bool res = 1;
  for (int i = 0; i < w; i++)
    res &= mp[h - 1][i] == '.';
  return res;
}

bool solve(int x, int y) {
  mp = tmp;
  if (x + 1 < w)
    swap(mp[y][x], mp[y][x + 1]);
  return cal();
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++)
    cin >> mp[i], mp[i] += '?';
  mp[h] = "???????????????????????????????";
  tmp = mp;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (solve(j, i)) {
        cout << "YES" << endl;
        return 0;
      }

  cout << "NO" << endl;
  return 0;
}