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

#include <iostream>
#include <queue>
using namespace std;

int x, y;

struct grid {
  char g[11][11];
  int count;
};

void DFS(grid &gr, char firstcell, char changecolor, int posx, int posy) {

  if (posx < 0 || posx >= x || posy < 0 || posy >= y ||
      gr.g[posx][posy] != firstcell) {
    // if(gr.g[posx][posy] != firstcell){
    return;
  }

  gr.g[posx][posy] = changecolor;

  DFS(gr, firstcell, changecolor, posx + 1, posy);
  DFS(gr, firstcell, changecolor, posx - 1, posy);
  DFS(gr, firstcell, changecolor, posx, posy + 1);
  DFS(gr, firstcell, changecolor, posx, posy - 1);
}

int main() {

  while (1) {

    cin >> x >> y;

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

    grid start;

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        cin >> start.g[j][i];
      }
    }

    char firstcell = start.g[0][0];

    bool flag = true;

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        if (start.g[j][i] != firstcell) {
          flag = false;
          break;
        }
      }
      if (!flag) {
        break;
      }
    }
    if (flag) {
      cout << 0 << endl;
      continue;
    }

    start.count = 0;

    queue<grid> q;

    q.push(start);

    while (1) {

      grid BeforeGrid = q.front();
      q.pop();

      BeforeGrid.count++;

      grid tmp = BeforeGrid;

      firstcell = tmp.g[0][0];

      if (firstcell != 'R') {
        DFS(tmp, firstcell, 'R', 0, 0);

        flag = true;
        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if (tmp.g[j][i] != 'R') {
              flag = false;
              break;
            }
          }
          if (!flag) {
            break;
          }
        }
        if (flag) {
          cout << tmp.count << endl;
          break;
        }

        q.push(tmp);
        tmp = BeforeGrid;
      }

      if (firstcell != 'G') {
        DFS(tmp, firstcell, 'G', 0, 0);

        flag = true;
        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if (tmp.g[j][i] != 'G') {
              flag = false;
              break;
            }
          }
          if (!flag) {
            break;
          }
        }
        if (flag) {
          cout << tmp.count << endl;
          break;
        }

        q.push(tmp);
        tmp = BeforeGrid;
      }

      if (firstcell != 'B') {
        DFS(tmp, firstcell, 'B', 0, 0);

        flag = true;
        for (int i = 0; i < y; i++) {
          for (int j = 0; j < x; j++) {
            if (tmp.g[j][i] != 'B') {
              flag = false;
              break;
            }
          }
          if (!flag) {
            break;
          }
        }
        if (flag) {
          cout << tmp.count << endl;
          break;
        }

        q.push(tmp);
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

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

int x, y;

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

  if (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);
}

int main() {

  while (1) {

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

    cin >> x >> y;

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

    vector<vector<char>> v;

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

    for (int i = 0; i < x; i++) {
      v[i].resize(static_cast<unsigned>(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];
      }
    }

    queue<pair<vector<vector<char>>, pair<int, char>>> q;

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

    while (!q.empty()) {

      int count = q.front().second.first;

      char beforefilling = q.front().second.first;

      v = q.front().first;

      q.pop();

      char base = v[0][0];

      bool flag = true;

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

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

      count++;

      vector<vector<char>> tmp;

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

      for (int i = 0; i < x; i++) {
        tmp[i].resize(static_cast<unsigned>(y));
      }

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

      if (beforefilling != 'R') {
        DFS(v, base, 'R', 0, 0);
        q.push(make_pair(v, make_pair(count, 'R')));
        for (int i = 0; i < x; i++) {
          for (int j = 0; j < y; j++) {
            v[i][j] = tmp[i][j];
          }
        }
      }

      if (beforefilling != 'G') {
        DFS(v, base, 'G', 0, 0);
        q.push(make_pair(v, make_pair(count, 'G')));
        for (int i = 0; i < x; i++) {
          for (int j = 0; j < y; j++) {
            v[i][j] = tmp[i][j];
          }
        }
      }

      if (beforefilling != 'B') {
        DFS(v, base, 'B', 0, 0);
        q.push(make_pair(v, make_pair(count, 'B')));
      }

      /*DFS(v,base,'R',0,0);
      q.push(make_pair(v,make_pair(count,'R')));
      for(int i=0;i<x;i++){
          for(int j=0;j<y;j++){
              v[i][j] = tmp[i][j];
          }
      }
      DFS(v,base,'G',0,0);
      q.push(make_pair(v,count));
      for(int i=0;i<x;i++){
          for(int j=0;j<y;j++){
              v[i][j] = tmp[i][j];
          }
      }
      DFS(v,base,'B',0,0);
      q.push(make_pair(v,count));*/
    }
  }

  return 0;
}