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

#include <cstdio>
#include <cstring>

int h, w, n;
inline bool islower(char c) { return ('a' <= c) && (c <= 'z'); }
inline bool isupper(char c) { return ('A' <= c) && (c <= 'Z'); }
inline char tolower(char c) { return (isupper(c) ? c - 'A' + 'a' : c); }

bool search(const char m[40][40]) {
  char cpy[40][40];
  memcpy(cpy, m, 1600);
  for (int k = 0; k < 500; k++) {
    bool flag = false;
    for (int l = 0; l < 50; l++)
      for (int x = 0; x < w; x++)
        for (int y = 0; y < h - 1; y++) {
          if (cpy[y + 1][x] == '.')
            cpy[y + 1][x] = cpy[y][x], cpy[y][x] = '.';
        }
    for (int x = 0; x < w; x++)
      for (int y = 0; y < h; y++) {
        int i = 0;
        if (cpy[y][x] == '.')
          continue;
        while (tolower(cpy[y][x]) == tolower(cpy[y + i][x]))
          i++;
        if (n <= i) {
          flag = true;
          for (int j = 0; j < i; j++)
            cpy[y + j][x] = tolower(cpy[y + j][x]);
        }
        y += i - 1;
      }
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
        int i = 0;
        if (cpy[y][x] == '.')
          continue;
        while (tolower(cpy[y][x]) == tolower(cpy[y][x + i]))
          i++;
        if (n <= i) {
          flag = true;
          for (int j = 0; j < i; j++)
            cpy[y][x + j] = tolower(cpy[y][x + j]);
        }
        x += i - 1;
      }
    if (!flag)
      break;
    for (int x = 0; x < w; x++)
      for (int y = 0; y < h; y++) {
        if (islower(cpy[y][x]))
          cpy[y][x] = '.';
      }
  }
  for (int y = 0; y < h; y++)
    printf("", cpy[y]);
  for (int x = 0; x < w; x++)
    for (int y = 0; y < h; y++) {
      if (cpy[y][x] != '.')
        return false;
    }
  return true;
}

bool swap(const char m[40][40]) {
  char cpy[40][40];
  for (int x = 0; x < w; x++)
    for (int y = 0; y < h; y++) {
      if (m[y][x + 1]) {
        memcpy(cpy, m, 1600);
        cpy[y][x + 1] = m[y][x];
        cpy[y][x] = m[y][x + 1];
        if (search(cpy))
          return true;
      }
    }
  return false;
}

int main(void) {
  char map[40][40];
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; i++)
    scanf("%s", map[i]);
  printf(swap(map) ? "YES\\n" : "NO\\n");

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

int h, w, n;
bool islower(char c) { return ('a' <= c) && (c <= 'z'); }
bool isupper(char c) { return ('A' <= c) && (c <= 'Z'); }
char tolower(char c) { return (isupper(c) ? c - 'A' + 'a' : c); }

bool search(const char m[40][40]) {
  char cpy[40][40];
  memcpy(cpy, m, 1600);
  for (int k = 0; k < 300; k++) {
    bool flag = false;
    for (int l = 0; l < 100; l++)
      for (int x = 0; x < w; x++)
        for (int y = 0; y < h - 1; y++) {
          if (cpy[y + 1][x] == '.')
            cpy[y + 1][x] = cpy[y][x], cpy[y][x] = '.';
        }
    for (int x = 0; x < w; x++)
      for (int y = 0; y < h; y++) {
        int i = 0;
        while (tolower(cpy[y][x]) == tolower(cpy[y + i][x]))
          i++;
        if (n <= i) {
          flag = true;
          for (int j = 0; j < i; j++)
            cpy[y + j][x] = tolower(cpy[y + j][x]);
        }
        y += i - 1;
      }
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
        int i = 0;
        while (tolower(cpy[y][x]) == tolower(cpy[y][x + i]))
          i++;
        if (n <= i) {
          flag = true;
          for (int j = 0; j < i; j++)
            cpy[y][x + j] = tolower(cpy[y][x + j]);
        }
        x += i - 1;
      }
    for (int x = 0; x < w; x++)
      for (int y = 0; y < h; y++) {
        if (islower(cpy[y][x]))
          cpy[y][x] = '.';
      }
    if (!flag)
      break;
  }
  for (int x = 0; x < w; x++)
    for (int y = 0; y < h; y++) {
      if (cpy[y][x] != '.')
        return false;
    }
  return true;
}

bool swap(const char m[40][40]) {
  char cpy[40][40];
  for (int x = 0; x < w; x++)
    for (int y = 0; y < h; y++) {
      if (m[y][x + 1]) {
        memcpy(cpy, m, 1600);
        cpy[y][x + 1] = m[y][x];
        cpy[y][x] = m[y][x + 1];
        if (search(cpy))
          return true;
      }
    }
  return false;
}

int main(void) {
  char map[40][40];
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; i++)
    scanf("%s", map[i]);
  printf(swap(map) ? "YES\\n" : "NO\\n");

  return 0;
}