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

#include <iostream>
using namespace std;

const int N = 1010;

int main() {
  int a, b;
  cin >> a >> b;
  char a11, a12, a21, a22, x[N][N], y;
  int j = 0;
  for (int k = 1; k <= a; k++) {
    for (int l = 1; l <= b; l++) {
      cin >> x[k][l];
    }
  }
  cin >> a11 >> a12 >> a21 >> a22;
  for (int k = 1; k <= a - 1; k++) {
    for (int l = 1; l <= b - 1; l++) {
      if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
          (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))
        j += 1;
    }
  }
  int q = j;
  int m, n, p;
  for (int k = 1; k <= a; k++) {
    for (int l = 1; l <= b; l++) {
      y = x[k][l];
      m = 0;
      if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
          (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))
        m += 1;
      if ((x[k - 1][l] == a11 && x[k - 1][l + 1] == a12) &&
          (x[k][l] == a21 && x[k][l + 1] == a22))
        m += 1;
      if ((x[k][l - 1] == a11 && x[k][l] == a12) &&
          (x[k + 1][l - 1] == a21 && x[k + 1][l] == a22))
        m += 1;
      if ((x[k - 1][l - 1] == a11 && x[k - 1][l] == a12) &&
          (x[k][l - 1] == a21 && x[k][l] == a22))
        m += 1;
      n = 0;
      x[k][l] = a11;
      if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
          (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))
        n += 1;
      if ((x[k - 1][l] == a11 && x[k - 1][l + 1] == a12) &&
          (x[k][l] == a21 && x[k][l + 1] == a22))
        n += 1;
      if ((x[k][l - 1] == a11 && x[k][l] == a12) &&
          (x[k + 1][l - 1] == a21 && x[k + 1][l] == a22))
        n += 1;
      if ((x[k - 1][l - 1] == a11 && x[k - 1][l] == a12) &&
          (x[k][l - 1] == a21 && x[k][l] == a22))
        n += 1;
      p = max(m, n);
      n = 0;
      x[k][l] = a12;
      if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
          (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))
        n += 1;
      if ((x[k - 1][l] == a11 && x[k - 1][l + 1] == a12) &&
          (x[k][l] == a21 && x[k][l + 1] == a22))
        n += 1;
      if ((x[k][l - 1] == a11 && x[k][l] == a12) &&
          (x[k + 1][l - 1] == a21 && x[k + 1][l] == a22))
        n += 1;
      if ((x[k - 1][l - 1] == a11 && x[k - 1][l] == a12) &&
          (x[k][l - 1] == a21 && x[k][l] == a22))
        n += 1;
      p = max(p, n);
      n = 0;
      x[k][l] = a21;
      if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
          (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))
        n += 1;
      if ((x[k - 1][l] == a11 && x[k - 1][l + 1] == a12) &&
          (x[k][l] == a21 && x[k][l + 1] == a22))
        n += 1;
      if ((x[k][l - 1] == a11 && x[k][l] == a12) &&
          (x[k + 1][l - 1] == a21 && x[k + 1][l] == a22))
        n += 1;
      if ((x[k - 1][l - 1] == a11 && x[k - 1][l] == a12) &&
          (x[k][l - 1] == a21 && x[k][l] == a22))
        n += 1;
      p = max(p, n);
      n = 0;
      x[k][l] = a22;
      if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
          (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))
        n += 1;
      if ((x[k - 1][l] == a11 && x[k - 1][l + 1] == a12) &&
          (x[k][l] == a21 && x[k][l + 1] == a22))
        n += 1;
      if ((x[k][l - 1] == a11 && x[k][l] == a12) &&
          (x[k + 1][l - 1] == a21 && x[k + 1][l] == a22))
        n += 1;
      if ((x[k - 1][l - 1] == a11 && x[k - 1][l] == a12) &&
          (x[k][l - 1] == a21 && x[k][l] == a22))
        n += 1;
      p = max(p, n);
      j = max(j, q + p - m);
      x[k][l] = y;
    }
  }
  cout << j << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  char a11, a12, a21, a22, x[a][b], y;
  int j = 0;
  for (int k = 0; k < a; k++) {
    for (int l = 0; l < b; l++) {
      cin >> x[k][l];
    }
  }
  cin >> a11 >> a12 >> a21 >> a22;
  for (int k = 0; k < a - 1; k++) {
    for (int l = 0; l < b - 1; l++) {
      if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
          (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))
        j += 1;
    }
  }

  int m;
  for (int k = 0; k < a; k++) {
    for (int l = 0; l < b; l++) {
      y = x[k][l];
      x[k][l] = a11;
      m = 0;
      for (int k = 0; k < a - 1; k++) {
        for (int l = 0; l < b - 1; l++) {
          if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
              (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))

            m += 1;
        }
      }
      j = max(j, m);
      x[k][l] = y;
    }
  }
  for (int k = 0; k < a; k++) {
    for (int l = 0; l < b; l++) {
      y = x[k][l];
      x[k][l] = a12;
      m = 0;
      for (int k = 0; k < a - 1; k++) {
        for (int l = 0; l < b - 1; l++) {
          if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
              (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))

            m += 1;
        }
      }
      j = max(j, m);
      x[k][l] = y;
    }
  }
  for (int k = 0; k < a; k++) {
    for (int l = 0; l < b; l++) {
      y = x[k][l];
      x[k][l] = a21;
      m = 0;
      for (int k = 0; k < a - 1; k++) {
        for (int l = 0; l < b - 1; l++) {
          if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
              (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))

            m += 1;
        }
      }
      j = max(j, m);
      x[k][l] = y;
    }
  }
  for (int k = 0; k < a; k++) {
    for (int l = 0; l < b; l++) {
      y = x[k][l];
      x[k][l] = a22;
      m = 0;
      for (int k = 0; k < a - 1; k++) {
        for (int l = 0; l < b - 1; l++) {
          if ((x[k][l] == a11 && x[k][l + 1] == a12) &&
              (x[k + 1][l] == a21 && x[k + 1][l + 1] == a22))

            m += 1;
        }
      }
      j = max(j, m);
      x[k][l] = y;
    }
  }
  cout << j << "\\n";
  return 0;
}