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

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

int x, y;

struct sc {
  char cs[11][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;
}

Error: MLE

Fixed submission:

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

int x, y;

void DFS(vector<string> &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;
    }

    vector<string> v(x);

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

    for (int i = 0; i < x; i++) {
      v[i].resize(y);
    }

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        // char c;
        /*scanf(" %c",&c);
        v[j][i] = c;*/
        cin >> v[j][i];
        // v[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<vector<string>, 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[i][j] << ' ';
          }cout << endl;
      }cout << endl;*/

      q.pop();

      count++;

      vector<string> tmp(v);

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

      /*for(int i=0;i<y;i++){
          tmp[i] += v[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[i] = tmp[i];
        }*/
        v = tmp;
      }

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

        // char base = v[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;
}