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

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

int h, w, n;
string mas[30];
int dy[] = {0, 1}, dx[] = {1, 0};

int rec(
    bool flag = false) { // 操作一回だった!! //縦は入れ替えれなかった(誤読不可避
  int cnt = 0;
  for (int i = 0; i < h; ++i)
    cnt += count(mas[i].begin(), mas[i].end(), '.');
  if (flag)
    return (cnt == h * w);

  string prev_map[30];
  for (int i = 0; i < h; ++i)
    prev_map[i] = mas[i];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int k = 0; k < 1; ++k) {
        int ny = i + dy[k], nx = j + dx[k];
        if (ny >= h || nx >= w || nx < 0 || ny < 0)
          continue;
        if (mas[ny][nx] == mas[i][j])
          continue;
        swap(mas[ny][nx], mas[i][j]);
        for (int l = h - 1; l >= 0; l--) {
          for (int m = 0; m < w; m++) {
            if (mas[l][m] == '.') {
              for (int o = l - 1; o >= 0; o--) {
                if (mas[o][m] != '.') {
                  mas[l][m] = mas[o][m];
                  mas[o][m] = '.';
                  break;
                }
              }
            }
          }
        }
        bool first = false, down = true;
        while (down) {
          bool killed[30][30] = {{}};
          down = false;
          for (int l = 0; l < h; l++) {
            int cnt = 1;
            for (int m = 1; m < w; m++) {
              if (isalpha(mas[l][m])) {
                if (mas[l][m] == mas[l][m - 1]) {
                  cnt++;
                  if (cnt >= n) {
                    if (cnt == n) {
                      for (int o = m - 1, p = 0; p < cnt - 1; --o, ++p) {
                        killed[l][o] = true;
                      }
                    }
                    killed[l][m] = true;

                    down = true, first = true;
                  }
                } else {
                  cnt = 1;
                }
              }
            }
          }
          for (int l = 0; l < w; l++) {
            int cnt = 1;
            for (int m = 1; m < h; m++) {
              if (isalpha(mas[m][l])) {
                if (mas[m][l] == mas[m - 1][l]) {
                  cnt++;
                  if (cnt >= n) {
                    if (cnt == n) {
                      for (int o = m - 1, p = 0; p < cnt - 1; --o, ++p) {
                        killed[o][l] = true;
                      }
                    }
                    killed[m][l] = true;

                    down = true, first = true;
                  }
                } else {
                  cnt = 1;
                }
              }
            }
          }

          if (down) {
            for (int l = 0; l < h; l++) {
              for (int m = 0; m < w; m++) {
                if (killed[l][m])
                  mas[l][m] = '.';
              }
            }
            for (int l = h - 1; l >= 0; l--) {
              for (int m = 0; m < w; m++) {
                if (mas[l][m] == '.') {
                  for (int o = l - 1; o >= 0; o--) {
                    if (mas[o][m] != '.') {
                      mas[l][m] = mas[o][m];
                      mas[o][m] = '.';
                      break;
                    }
                  }
                }
              }
            }
          }
          //         for(int l = 0; l < h; l++) cout << mas[l] << endl;
          //       cout << endl;
        }

        if (first) {
          // for(int l = 0; l < h; ++l) cout << mas[l] << endl;
          //   cout << endl;
          if (rec(true))
            return true;
        }
        for (int l = 0; l < h; l++)
          mas[l] = prev_map[l];
      }
    }
  }
  return false;
}

int main() {

  cin >> h >> w >> n;
  for (int i = 0; i < h; i++)
    cin >> mas[i];
  if (rec())
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Error: TLE

Fixed submission:

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

int h, w, n;
string mas[30];
int dy[] = {0, 1}, dx[] = {1, 0};

bool rec() {
  int cnt = 0;
  for (int i = 0; i < h; ++i)
    cnt += count(mas[i].begin(), mas[i].end(), '.');
  if (cnt == h * w)
    return true;

  string prev_map[30];
  for (int i = 0; i < h; ++i)
    prev_map[i] = mas[i];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int k = 0; k < 2; ++k) {
        int ny = i + dy[k], nx = j + dx[k];
        if (ny >= h || nx >= w || nx < 0 || ny < 0)
          continue;
        if (mas[ny][nx] == mas[i][j])
          continue;
        swap(mas[ny][nx], mas[i][j]);
        bool first = false, down = true;
        while (down) {
          bool killed[30][30] = {{}};
          down = false;
          for (int l = 0; l < h; l++) {
            int cnt = 1;
            for (int m = 1; m < w; m++) {
              if (isalpha(mas[l][m])) {
                if (mas[l][m] == mas[l][m - 1]) {
                  cnt++;
                  if (cnt >= n) {
                    if (cnt == n) {
                      for (int o = m - 1, p = 0; p < cnt - 1; --o, ++p) {
                        killed[l][o] = true;
                      }
                      killed[l][m] = true;
                    }
                    down = true, first = true;
                  }
                } else {
                  cnt = 1;
                }
              }
            }
          }
          for (int l = 0; l < w; l++) {
            int cnt = 1;
            for (int m = 1; m < h; m++) {
              if (isalpha(mas[m][l])) {
                if (mas[m][l] == mas[m - 1][l]) {
                  cnt++;
                  if (cnt >= n) {
                    if (cnt == n) {
                      for (int o = m - 1, p = 0; p < cnt - 1; --o, ++p) {
                        killed[o][l] = true;
                      }
                      killed[m][l] = true;
                    }
                    down = true, first = true;
                  }
                } else {
                  cnt = 1;
                }
              }
            }
          }

          if (down) {
            for (int i = 0; i < h; i++) {
              for (int j = 0; j < w; j++) {
                if (killed[i][j])
                  mas[i][j] = '.';
              }
            }
            for (int i = h - 1; i >= 0; i--) {
              for (int j = 0; j < w; j++) {
                if (killed[i][j] && mas[i][j] == '.') {
                  for (int k = i - 1; k >= 0; k--) {
                    if (mas[k][j] != '.') {
                      mas[i][j] = mas[k][j];
                      break;
                    }
                  }
                }
              }
            }
          }
        }
        if (first) {
          if (rec())
            return true;
        }
        swap(mas[i][j], mas[ny][nx]);

        if (first) {
          for (int l = 0; l < h; l++)
            mas[l] = prev_map[l];
        }
      }
    }
  }
  return false;
}

int main() {

  cin >> h >> w >> n;
  for (int i = 0; i < h; i++)
    cin >> mas[i];
  if (rec())
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}