Problem p00243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int x, y;

void DFS(array<array<char, 11>, 11> &v, char base, char c, int xp, int yp) {

  if (xp < 0 || yp < 0 || xp >= x || yp >= y || v[xp][yp] != base) {
    return;
  }

  v[xp][yp] = c;

  DFS(v, base, c, xp + 1, yp);
  DFS(v, base, c, xp, yp + 1);
  DFS(v, base, c, xp - 1, yp);
  DFS(v, base, c, xp, yp - 1);
}

int main() {

  while (1) {

    // scanf(" %d %d",&x,&y);

    cin >> x >> y;

    if (x == 0 && y == 0) {
      break;
    }

    array<array<char, 11>, 11> v;

    // v.cs.resize(static_cast<unsigned>(x));

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        // char c;
        /*scanf(" %c",&c);
        v.cs[j][i] = c;*/
        cin >> v[j][i];
        // v.cs[j][i] = c;
      }
    }

    bool flag = true;

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        if (v[0][0] != v[j][i]) {
          flag = false;
          break;
        }
      }
    }

    if (flag) {
      cout << 0 << endl;
      continue;
    }

    queue<pair<array<array<char, 11>, 11>, int>> q;

    q.push(make_pair(v, 0));

    while (!q.empty()) {

      int count = q.front().second;

      v = q.front().first;

      /*for(int i=0;i<y;i++){
          for(int j=0;j<x;j++){
              cout << v.cs[i][j] << ' ';
          }cout << endl;
      }cout << endl;*/

      q.pop();

      count++;

      array<array<char, 11>, 11> tmp;

      // tmp.resize(static_cast<unsigned>(x));

      /*for(int i=0;i<y;i++){
          tmp[i] += v.cs[i];
      }*/

      tmp = v;

      if (v[0][0] != 'R') {

        DFS(v, v[0][0], 'R', 0, 0);

        flag = true;

        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if ('R' != v[j][i]) {
              flag = false;
              break;
            }
          }
        }

        if (flag) {
          cout << count << endl;
          break;
        }

        q.push(make_pair(v, count));
        /*for(int i=0;i<y;i++){
            v[i] = tmp[i];
        }*/
        v = tmp;
      }

      if (v[0][0] != 'G') {

        DFS(v, v[0][0], 'G', 0, 0);

        flag = true;

        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if ('G' != v[j][i]) {
              flag = false;
              break;
            }
          }
        }

        if (flag) {
          cout << count << endl;
          break;
        }

        q.push(make_pair(v, count));
        /*for(int i=0;i<y;i++){
            v.cs[i] = tmp[i];
        }*/
        v = tmp;
      }

      if (v[0][0] != 'B') {

        // char base = v.cs[0][0];
        DFS(v, v[0][0], 'B', 0, 0);

        flag = true;

        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if ('B' != v[j][i]) {
              flag = false;
              break;
            }
          }
        }

        if (flag) {
          cout << count << endl;
          break;
        }

        q.push(make_pair(v, count));
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int x, y;

struct sc {
  string cs[11];

  sc() {
    for (int i = 0; i < 11; i++) {
      cs[i].resize(11);
    }
  }
};

void DFS(sc &v, char base, char c, int xp, int yp) {

  if (xp < 0 || yp < 0 || xp >= x || yp >= y || v.cs[xp][yp] != base) {
    return;
  }

  v.cs[xp][yp] = c;

  DFS(v, base, c, xp + 1, yp);
  DFS(v, base, c, xp, yp + 1);
  DFS(v, base, c, xp - 1, yp);
  DFS(v, base, c, xp, yp - 1);
}

int main() {

  while (1) {

    // scanf(" %d %d",&x,&y);

    cin >> x >> y;

    if (x == 0 && y == 0) {
      break;
    }

    sc v;

    // v.cs.resize(static_cast<unsigned>(x));

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        // char c;
        /*scanf(" %c",&c);
        v.cs[j][i] = c;*/
        cin >> v.cs[j][i];
        // v.cs[j][i] = c;
      }
    }

    bool flag = true;

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        if (v.cs[0][0] != v.cs[j][i]) {
          flag = false;
          break;
        }
      }
    }

    if (flag) {
      cout << 0 << endl;
      continue;
    }

    queue<pair<sc, int>> q;

    q.push(make_pair(v, 0));

    while (!q.empty()) {

      int count = q.front().second;

      v = q.front().first;

      /*for(int i=0;i<y;i++){
          for(int j=0;j<x;j++){
              cout << v.cs[i][j] << ' ';
          }cout << endl;
      }cout << endl;*/

      q.pop();

      count++;

      sc tmp;

      // tmp.resize(static_cast<unsigned>(x));

      /*for(int i=0;i<y;i++){
          tmp[i] += v.cs[i];
      }*/

      tmp = v;

      if (v.cs[0][0] != 'R') {

        DFS(v, v.cs[0][0], 'R', 0, 0);

        flag = true;

        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if ('R' != v.cs[j][i]) {
              flag = false;
              break;
            }
          }
        }

        if (flag) {
          cout << count << endl;
          break;
        }

        q.push(make_pair(v, count));
        /*for(int i=0;i<y;i++){
            v[i] = tmp[i];
        }*/
        v = tmp;
      }

      if (v.cs[0][0] != 'G') {

        DFS(v, v.cs[0][0], 'G', 0, 0);

        flag = true;

        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if ('G' != v.cs[j][i]) {
              flag = false;
              break;
            }
          }
        }

        if (flag) {
          cout << count << endl;
          break;
        }

        q.push(make_pair(v, count));
        /*for(int i=0;i<y;i++){
            v.cs[i] = tmp[i];
        }*/
        v = tmp;
      }

      if (v.cs[0][0] != 'B') {

        // char base = v.cs[0][0];
        DFS(v, v.cs[0][0], 'B', 0, 0);

        flag = true;

        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if ('B' != v.cs[j][i]) {
              flag = false;
              break;
            }
          }
        }

        if (flag) {
          cout << count << endl;
          break;
        }

        q.push(make_pair(v, count));
      }
    }
  }

  return 0;
}