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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
int h, w, n;

char fi[100][100];
bool d(char fi[100][100]) {
  char fi2[100][100];
  memcpy(fi2, fi, 10000);
  bool flag = false;
  for (int i = 0; i < h; i++) {
    int con = 0;
    for (int j = 1; j < w; j++) {
      if (fi2[i][j - 1] == fi2[i][j] && fi2[i][j - 1] != '.') {
        if (con == 0)
          con++;
        con++;
        if (con >= n) {
          flag = true;
          for (int k = j - n + 1; k <= j; k++)
            fi[i][k] = '.';
        }
      } else
        con = 0;
    }
  }

  for (int i = 0; i < w; i++) {
    int con = 1;
    for (int j = 1; j < h; j++) {
      if (fi2[j - 1][i] == fi2[j][i] && fi2[j - 1][i] != '.') {
        if (con == 0)
          con++;
        con++;
        if (con >= n) {
          flag = true;
          for (int k = j - n + 1; k <= j; k++)
            fi[k][i] = '.';
        }
      } else
        con = 1;
    }
  }
  return flag;
}

void f(char fi[100][100]) {
  for (int i = 0; i < w; i++) {
    while (1) {
      bool flag = true;
      for (int j = 0; j < h - 1; j++) {
        if (fi[j][i] != '.' && fi[j + 1][i] == '.') {
          flag = false;
          for (int k = j; k >= 0; k--) {
            fi[k + 1][i] = fi[k][i];
          }
          fi[0][i] = '.';
        }
      }
      if (flag)
        break;
    }
  }
}

bool s(int y, int x) {
  char fi2[100][100];
  memcpy(fi2, fi, sizeof(fi));

  char ex = fi2[y][x];
  fi2[y][x] = fi2[y][x + 1];
  fi2[y][x + 1] = ex;

  f(fi2);
  while (d(fi2)) {
    f(fi2);
  }

  bool flag = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (fi2[i][j] != '.')
        flag = false;
    }
  }
  return flag;
}

int main() {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; i++)
    scanf("%s", &fi[i][0]);
  bool flag = false;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (s(i, j)) {
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  if (flag)
    printf("YES\\n");
  else
    printf("NO\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
int h, w, n;

char fi[100][100];
bool d(char fi[100][100]) {
  char fi2[100][100];
  memcpy(fi2, fi, 10000);
  bool flag = false;
  for (int i = 0; i < h; i++) {
    int con = 1;
    for (int j = 1; j < w; j++) {
      if (fi2[i][j - 1] == fi2[i][j] && fi2[i][j - 1] != '.') {
        con++;
        if (con >= n) {
          flag = true;
          for (int k = j - n + 1; k <= j; k++)
            fi[i][k] = '.';
        }
      } else
        con = 1;
    }
  }

  for (int i = 0; i < w; i++) {
    int con = 1;
    for (int j = 1; j < h; j++) {
      if (fi2[j - 1][i] == fi2[j][i] && fi2[j - 1][i] != '.') {
        con++;
        if (con >= n) {
          flag = true;
          for (int k = j - n + 1; k <= j; k++)
            fi[k][i] = '.';
        }
      } else
        con = 1;
    }
  }
  return flag;
}

void f(char fi[100][100]) {
  for (int i = 0; i < w; i++) {
    bool flag = true;
    while (1) {
      for (int j = 0; j < h - 1; j++) {
        if (fi[j][i] != '.' && fi[j + 1][i] == '.') {
          flag = false;
          for (int k = j; k > 0; k--) {
            fi[j + 1][i] = fi[j][i];
          }
          fi[0][i] = '.';
        }
      }
      if (flag)
        break;
    }
  }
}

bool s(int y, int x) {
  char fi2[100][100];
  memcpy(fi2, fi, sizeof(fi));

  char ex = fi2[y][x];
  fi2[y][x] = fi2[y][x + 1];
  fi2[y][x + 1] = ex;

  while (d(fi2)) {
    f(fi2);
  }

  bool flag = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (fi2[i][j] != '.')
        flag = false;
    }
  }
  return flag;
}

int main() {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; i++)
    scanf("%s", &fi[i][0]);
  bool flag = false;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (fi[i][j] != '.' && fi[i][j + 1] != '.' && s(i, j)) {
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  if (flag)
    printf("YES\\n");
  else
    printf("NO\\n");
  return 0;
}