Problem p01349 (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 repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << 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, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int h, w, n;
char origin[30][30];
char tmp[2][30][30];

bool check() {
  rep(i, h) rep(j, w) tmp[0][i][j] = origin[i][j];

  rep(i, w) repr(j, h - 1) {
    if (tmp[0][j][i] != '.') {
      int k = j + 1;
      while (k < h && tmp[0][k][i] == '.')
        swap(tmp[0][k - 1][i], tmp[0][k][i]), k++;
    }
  }
  rep(i, h) rep(j, w) tmp[1][i][j] = tmp[0][i][j];
  // rep(i, h)rep(j, w) cout << tmp[0][i][j] << ((j == w - 1) ? "\\n" : " ");
  // cout << endl;

  int p = 0;
  while (true) {
    bool ok = false;
    rep(i, h) {
      int flag = 1;
      char c = '.';
      rep(j, w) {
        if (tmp[p % 2][i][j] == c && tmp[p % 2][i][j] != '.')
          flag++;
        else {
          if (flag >= n) {
            rep(k, flag) tmp[(p + 1) % 2][i][j - 1 - k] = '.';
            ok = true;
          }
          flag = 1, c = tmp[p % 2][i][j];
        }
      }
      if (flag >= n) {
        rep(k, flag) tmp[(p + 1) % 2][i][w - 1 - k] = '.';
        ok = true;
      }
    }
    rep(j, w) {
      int flag = 1;
      char c = '.';
      rep(i, h) {
        if (tmp[p % 2][i][j] == c && tmp[p % 2][i][j] != '.')
          flag++;
        else {
          if (flag >= n) {
            rep(k, flag) tmp[(p + 1) % 2][i - 1 - k][j] = '.';
            ok = true;
          }
          flag = 1, c = tmp[p % 2][i][j];
        }
      }
      if (flag >= n) {
        // cout << j << " " << flag << endl;
        rep(k, flag) tmp[(p + 1) % 2][h - 1 - k][j] = '.';
        // rep(i, h)rep(j, w) cout << tmp[(p + 1) % 2][i][j] << ((j == w - 1) ?
        // "\\n" : " ");

        ok = true;
      }
    }

    if (!ok)
      break;
    p++;
    rep(i, w) repr(j, h - 1) {
      if (tmp[p % 2][j][i] != '.') {
        int k = j + 1;
        while (k < h && tmp[p % 2][k][i] == '.')
          swap(tmp[p % 2][k - 1][i], tmp[p % 2][k][i]), k++;
      }
    }

    rep(i, h) rep(j, w) tmp[(p + 1) % 2][i][j] = tmp[p % 2][i][j];

    // rep(i, h)rep(j, w) cout << tmp[p % 2][i][j] << ((j == w - 1) ? "\\n" : "
    // "); cout << endl;
  }
  // cout << "-----------------------------------------------------" << endl;
  bool ok = true;
  rep(i, h) rep(j, w) if (tmp[p % 2][i][j] != '.') ok = false;
  return ok;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> h >> w >> n;

  rep(i, h) rep(j, w) cin >> origin[i][j];

  bool ok = false;
  rep(i, h) {
    rep(j, w - 1) {
      if (origin[i][j + 1] != '.' || origin[i][j] != '.') {
        swap(origin[i][j], origin[i][j + 1]);
        ok = check();
        if (ok)
          break;
        swap(origin[i][j], origin[i][j + 1]);
      }
    }
    if (ok)
      break;
  }

  if (ok)
    cout << "YES" << endl;
  else
    cout << "NO" << 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 repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << 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, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int h, w, n;
char origin[30][30];
char tmp[2][30][30];

bool check() {
  rep(i, h) rep(j, w) tmp[0][i][j] = tmp[1][i][j] = origin[i][j];

  int p = 0;
  while (true) {
    bool ok = false;
    rep(i, h) {
      int flag = 1;
      char c = '.';
      rep(j, w) {
        if (tmp[p % 2][i][j] == c && tmp[p % 2][i][j] != '.')
          flag++;
        else {
          if (flag >= n) {
            repr(k, n) tmp[(p + 1) % 2][i][j - 1 - k] = '.';
            ok = true;
          }
          flag = 1, c = tmp[p % 2][i][j];
        }
      }
      if (flag >= n) {
        repr(k, n) tmp[(p + 1) % 2][i][w - 1 - k] = '.';
        ok = true;
      }
    }
    rep(j, w) {
      int flag = 1;
      char c = '.';
      rep(i, h) {
        if (tmp[p % 2][i][j] == c && tmp[p % 2][i][j] != '.')
          flag++;
        else {
          if (flag >= n) {
            repr(k, n) tmp[(p + 1) % 2][i - 1 - k][j] = '.';
            ok = true;
          }
          flag = 1, c = tmp[p % 2][i][j];
        }
      }
      if (flag >= n) {
        repr(k, n) tmp[(p + 1) % 2][h - 1 - k][j] = '.';
        ok = true;
      }
    }

    if (!ok)
      break;
    p++;
    rep(i, w) repr(j, h - 1) {
      if (tmp[p % 2][j][i] != '.') {
        int k = j + 1;
        while (k < h && tmp[p % 2][k][i] == '.')
          swap(tmp[p % 2][k - 1][i], tmp[p % 2][k][i]), k++;
      }
    }
  }

  bool ok = true;
  rep(i, h) rep(j, w) if (tmp[p % 2][i][j] != '.') ok = false;
  return ok;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> h >> w >> n;

  rep(i, h) rep(j, w) cin >> origin[i][j];

  bool ok = false;
  rep(i, h) {
    rep(j, w - 1) {
      if (origin[i][j + 1] != '.') {
        swap(origin[i][j], origin[i][j + 1]);
        ok = check();
        if (ok)
          break;
        swap(origin[i][j], origin[i][j + 1]);
      }
    }
    if (ok)
      break;
  }

  if (ok)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}