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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

int h, w, n;
string s[31];
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};

bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

bool ok() {
  bool f[31][31];
  string t[31];
  memset(f, false, sizeof(f));
  rep(i, 0, h) t[i] = s[i];
  for (int cnt = 0;; cnt++) {
    bool stop = true;
    rep(x, 0, w) {
      repb(y, h - 1, 0) {
        if (f[y][x])
          t[y][x] = '.';
        if (t[y][x] == '.')
          continue;
        int id = y;
        rep(i, y + 1, h) {
          if (t[i][x] != '.')
            break;
          else if (t[i][x] == '.')
            id = i;
        }
        char tmp = t[y][x];
        // if(f[y][x]) tmp = '.';
        t[y][x] = '.';
        t[id][x] = tmp;
      }
    }
    // cout<< cnt << endl;
    // rep(i, 0, h){
    //     rep(j, 0, w){
    //         cout << t[i][j];
    //     }
    //     cout << endl;
    // }
    bool ch = false;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (t[i][j] != '.')
          stop = false;
        if (f[i][j])
          ch = true;
      }
    }
    if (stop)
      return true;
    if (cnt > 1 && ch == false)
      return false;
    // if(cnt >= h + w) return false;
    memset(f, false, sizeof(f));
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (t[i][j] == '.')
          continue;
        rep(k, 1, n) {
          if (j + k >= w)
            continue;
          if (t[i][j] != t[i][j + k])
            break;
          if (k == n - 1) {
            rep(l, 0, n) f[i][j + l] = true;
          }
        }
      }
    }
    rep(i, 0, w) {
      rep(j, 0, h) {
        if (t[j][i] == '.')
          continue;
        rep(k, 1, n) {
          if (j + k >= h)
            continue;
          if (t[j][i] != t[j + k][i])
            break;
          if (k == n - 1) {
            rep(l, 0, n) f[j + l][i] = true;
          }
        }
      }
    }
  }
}

signed main() {
  cin >> h >> w >> n;
  rep(i, 0, h) cin >> s[i];
  rep(i, 0, h) {
    rep(j, 0, w) {
      rep(k, 2, 4) {
        int ny = i + dy[k];
        int nx = j + dx[k];
        if (!contain(ny, nx))
          continue;
        if (s[i][j] == '.' && s[ny][nx] == '.')
          continue;
        swap(s[i][j], s[ny][nx]);
        // cout << endl;
        // rep(l, 0, h){
        //     rep(m, 0, w){
        //         cout << s[l][m];
        //     }
        //     cout << endl;
        // }
        if (ok()) {
          cout << "YES" << endl;
          return 0;
        }
        swap(s[i][j], s[ny][nx]);
      }
    }
  }
  cout << "NO" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

int h, w, n;
string s[31];
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};

bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

bool ok() {
  bool f[31][31];
  string t[31];
  memset(f, false, sizeof(f));
  rep(i, 0, h) t[i] = s[i];
  for (int cnt = 0;; cnt++) {
    bool stop = true;
    rep(x, 0, w) {
      repb(y, h - 1, 0) {
        if (f[y][x])
          t[y][x] = '.';
        if (t[y][x] == '.')
          continue;
        int id = y;
        rep(i, y + 1, h) {
          if (t[i][x] != '.')
            break;
          else if (t[i][x] == '.')
            id = i;
        }
        char tmp = t[y][x];
        // if(f[y][x]) tmp = '.';
        t[y][x] = '.';
        t[id][x] = tmp;
      }
    }
    // cout<< cnt << endl;
    // rep(i, 0, h){
    //     rep(j, 0, w){
    //         cout << t[i][j];
    //     }
    //     cout << endl;
    // }
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (t[i][j] != '.')
          stop = false;
      }
    }
    if (stop)
      return true;
    if (cnt >= h * w)
      return false;
    memset(f, false, sizeof(f));
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (t[i][j] == '.')
          continue;
        rep(k, 1, n) {
          if (j + k >= w)
            continue;
          if (t[i][j] != t[i][j + k])
            break;
          if (k == n - 1) {
            rep(l, 0, n) f[i][j + l] = true;
          }
        }
      }
    }
    rep(i, 0, w) {
      rep(j, 0, h) {
        if (t[j][i] == '.')
          continue;
        rep(k, 1, n) {
          if (j + k >= h)
            continue;
          if (t[j][i] != t[j + k][i])
            break;
          if (k == n - 1) {
            rep(l, 0, n) f[j + l][i] = true;
          }
        }
      }
    }
  }
}

signed main() {
  cin >> h >> w >> n;
  rep(i, 0, h) cin >> s[i];
  rep(i, 0, h) {
    rep(j, 0, w) {
      rep(k, 2, 4) {
        int ny = i + dy[k];
        int nx = j + dx[k];
        if (!contain(ny, nx))
          continue;
        if (s[i][j] == '.' && s[ny][nx] == '.')
          continue;
        swap(s[i][j], s[ny][nx]);
        // cout << endl;
        // rep(l, 0, h){
        //     rep(m, 0, w){
        //         cout << s[l][m];
        //     }
        //     cout << endl;
        // }
        if (ok()) {
          cout << "YES" << endl;
          return 0;
        }
        swap(s[i][j], s[ny][nx]);
      }
    }
  }
  cout << "NO" << endl;
}