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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 100000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem2232 : Ennichi **/
int main() {
  int H, W, N;
  cin >> H >> W >> N;

  char field[H][W];
  rep(i, H) { cin >> field[i]; }

  bool flg = false;
  rep(oy, H) {
    rep(ox, W - 1) {
      char field2[H][W];
      memcpy(field2, field, sizeof(field));
      swap(field2[oy][ox], field2[oy][ox + 1]);

      for (int y = H - 2; y >= 0; y--) {
        for (int x = 0; x < W; x++) {
          int ty = y;
          while (ty < H - 1 && field2[ty + 1][x] == '.') {
            swap(field2[ty][x], field2[ty + 1][x]);
            ty++;
          }
        }
      }

      while (1) {
        bool update = false;
        bool del[H][W];
        memset(del, 0, sizeof(del));

        rep(y, H) {
          rep(x, W) {
            char base = field2[y][x];
            int cntH = 1;

            if (base == '.')
              continue;

            for (int i = x + 1; i < W; i++) {
              if (base == field2[y][i])
                cntH++;
              else
                break;
            }

            int cntV = 1;
            for (int i = y + 1; i < H; i++) {
              if (base == field2[i][x])
                cntV++;
              else
                break;
            }

            if (cntH >= N) {
              update = true;
              for (int i = 0; i < cntH; i++) {
                del[y][x + i] = true;
              }
            }

            if (cntV >= N) {
              update = true;
              for (int i = 0; i < cntV; i++) {
                del[y + i][x] = true;
              }
            }
          }
        }

        rep(y, H) {
          rep(x, W) {
            if (del[y][x])
              field2[y][x] = '.';
          }
        }

        if (!update)
          break;

        for (int y = H - 2; y >= 0; y--) {
          for (int x = 0; x < W; x++) {
            int ty = y;
            while (ty < H - 1 && field2[ty + 1][x] == '.') {
              swap(field2[ty][x], field2[ty + 1][x]);
              ty++;
            }
          }
        }
      }

      for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
          if (field2[y][x] != '.')
            goto end;
        }
      }
      flg = true;
    end:;
    }
  }

  cout << (flg ? "YES" : "NO") << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 100000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem2232 : Ennichi **/
int main() {
  int H, W, N;
  while (cin >> H >> W >> N, H || W || N) {
    char field[H][W];
    rep(i, H) { cin >> field[i]; }

    bool flg = false;
    rep(oy, H) {
      rep(ox, W - 1) {
        char field2[H][W];
        memcpy(field2, field, sizeof(field));
        swap(field2[oy][ox], field2[oy][ox + 1]);
        bool first = true;

        while (1) {
          bool update = first;
          first = false;
          bool del[H][W];
          memset(del, 0, sizeof(del));

          rep(y, H) {
            rep(x, W) {
              char base = field2[y][x];
              int cntH = 1;

              if (base == '.')
                continue;

              for (int i = x + 1; i < W; i++) {
                if (base == field2[y][i])
                  cntH++;
                else
                  break;
              }

              int cntV = 1;
              for (int i = y + 1; i < H; i++) {
                if (base == field2[i][x])
                  cntV++;
                else
                  break;
              }

              if (cntH >= N) {
                for (int i = 0; i < cntH; i++) {
                  del[y][x + i] = true;
                }
              }

              if (cntV >= N) {
                for (int i = 0; i < cntV; i++) {
                  del[y + i][x] = true;
                }
              }
            }
          }

          rep(y, H) {
            rep(x, W) {
              if (del[y][x])
                field2[y][x] = '.';
            }
          }

          if (!update)
            break;
        }

        for (int y = 0; y < H; y++) {
          for (int x = 0; x < W; x++) {
            if (field2[y][x] != '.')
              goto end;
          }
        }
        flg = true;
      end:;
      }
    }

    cout << (flg ? "YES" : "NO") << endl;
  }
}