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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int h, w, n;
bool memo[31][31];
int dx[2] = {-1, 1};
int dy[2] = {0, 0};

void debug(const vector<string> &v) {
  cout << "[debug]" << endl;
  for (int y = 0; y < h; y++) {
    cout << v[y] << endl;
  }
  cout << endl;
}

bool is_clear(const vector<string> &v) {
  for (int x = 0; x < w; x++) {
    if (v[h - 1][x] != '.')
      return false;
  }
  return true;
}

bool fall(vector<string> &v) {
  bool change = false;
  for (int y = 1; y < h; y++) {
    for (int x = 0; x < w; x++) {
      while (true) {
        bool flag = false;
        if (y - 1 >= 0 && v[y - 1][x] != '.' && v[y][x] == '.') {
          swap(v[y - 1][x], v[y][x]);
          y--;
          change = true;
          flag = true;
        }
        if (!flag)
          break;
      }
    }
  }
  return change;
}

void check_row(int x, int y, const vector<string> &v, char c) {
  int cnt = 0;
  for (int sx = x; sx < x + n && sx < w; sx++) {
    if (v[y][sx] == c) {
      cnt++;
    } else {
      break;
    }
  }
  if (cnt == n) {
    for (int sx = x; sx < x + n && sx < w; sx++) {
      memo[y][sx] = true;
    }
  }
}

void check_col(int x, int y, const vector<string> &v, char c) {
  int cnt = 0;
  for (int sy = y; sy < y + n && sy < h; sy++) {
    if (v[sy][x] == c) {
      cnt++;
    } else {
      break;
    }
  }
  if (cnt == n) {
    for (int sy = y; sy < y + n && sy < h; sy++) {
      memo[sy][x] = true;
    }
  }
}

bool remove(vector<string> v) {
  fall(v);
  while (true) {
    bool change = false;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        memo[y][x] = false;
      }
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (v[y][x] != '.') {
          check_col(x, y, v, v[y][x]);
          check_row(x, y, v, v[y][x]);
        }
      }
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (memo[y][x]) {
          change = true;
          v[y][x] = '.';
        }
      }
    }
    fall(v);
    if (!change)
      break;
  }
  return is_clear(v);
}

bool solve(vector<string> v) {
  bool flag = false;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      for (int i = 0; i < 2; i++) {
        int mx = x + dx[i];
        int my = y + dy[i];
        if (mx < 0 || my < 0 || w <= mx || h <= my)
          continue;
        if (v[y][x] == v[my][mx])
          continue;

        swap(v[y][x], v[my][mx]);
        flag = remove(v);
        swap(v[y][x], v[my][mx]);
        if (flag) {
          return true;
        }
      }
    }
  }
  return false;
}

int main() {
  cin >> h >> w >> n;
  vector<string> v(h);
  for (int y = 0; y < h; y++) {
    cin >> v[y];
  }
  bool ans = solve(v);
  cout << (ans ? "YES" : "NO") << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int h, w, n;
bool memo[31][31];
bool col[31][31];
bool row[31][31];

void debug(const vector<string> &v) {
  cout << "[debug]" << endl;
  for (int y = 0; y < h; y++) {
    cout << v[y] << endl;
  }
  cout << endl;
}

bool is_clear(const vector<string> &v) {
  for (int x = 0; x < w; x++) {
    if (v[h - 1][x] != '.')
      return false;
  }
  return true;
}

bool fall(vector<string> &v) {
  bool change = false;
  for (int y = 1; y < h; y++) {
    for (int x = 0; x < w; x++) {
      while (true) {
        bool flag = false;
        if (y - 1 >= 0 && v[y - 1][x] != '.' && v[y][x] == '.') {
          swap(v[y - 1][x], v[y][x]);
          y--;
          change = true;
          flag = true;
        }
        if (!flag)
          break;
      }
    }
  }
  return change;
}

void check_row(int x, int y, const vector<string> &v, char c) {
  int cnt = 0;
  for (int sx = x; sx < x + n && sx < w; sx++) {
    if (v[y][sx] == c) {
      cnt++;
    } else {
      break;
    }
  }
  if (cnt == n) {
    for (int sx = x; sx < x + n && sx < w; sx++) {
      memo[y][sx] = row[y][sx] = true;
    }
  }
}

void check_col(int x, int y, const vector<string> &v, char c) {
  int cnt = 0;
  for (int sy = y; sy < y + n && sy < h; sy++) {
    if (v[sy][x] == c) {
      cnt++;
    } else {
      break;
    }
  }
  if (cnt == n) {
    for (int sy = y; sy < y + n && sy < h; sy++) {
      memo[sy][x] = col[sy][x] = true;
    }
  }
}

bool remove(vector<string> v) {
  fall(v);
  while (true) {
    bool change = false;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        memo[y][x] = col[y][x] = row[y][x] = false;
      }
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (v[y][x] != '.') {
          check_col(x, y, v, v[y][x]);
          check_row(x, y, v, v[y][x]);
        }
      }
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (memo[y][x]) {
          change = true;
          v[y][x] = '.';
        }
      }
    }
    fall(v);
    if (!change)
      break;
  }
  return is_clear(v);
}

bool solve(vector<string> v) {
  bool flag = false;
  for (int y1 = 0; y1 < h; y1++) {
    for (int x1 = 0; x1 < w; x1++) {
      for (int y2 = y1; y2 < h; y2++) {
        for (int x2 = x1 + 1; x2 < w; x2++) {
          if (v[y1][x1] == '.' && v[y2][x2] == '.')
            continue;
          if (v[y1][x1] == v[y2][x2])
            continue;

          swap(v[y2][x2], v[y1][x1]);
          flag = remove(v);
          swap(v[y2][x2], v[y1][x1]);
          if (flag)
            return true;
        }
      }
    }
  }
  return false;
}

int main() {
  cin >> h >> w >> n;
  vector<string> v(h);
  for (int y = 0; y < h; y++) {
    cin >> v[y];
  }
  bool ans = solve(v);
  cout << (ans ? "YES" : "NO") << endl;
}