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

#include <iostream>
#include <queue>
using namespace std;
class state {
public:
  int c[12][12];
  int time;
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int X, Y;
void DFS(state &copy, int x, int y, int oldcolor, int newcolor) {
  if (copy.c[x][y] != oldcolor)
    return;
  else {
    copy.c[x][y] = newcolor;
  }
  for (int i = 0; i < 4; i++) {
    DFS(copy, x + dx[i], y + dy[i], oldcolor, newcolor);
  }
  return;
}
int main() {
  while (true) {
    bool breakflg = false;
    state num;
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;
    for (int x = 0; x < 12; x++) {
      for (int y = 0; y < 12; y++) {
        num.c[x][y] == -1;
      }
    }
    for (int y = 1; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        char data;
        cin >> data;
        if (data == 'R')
          num.c[x][y] = 1;
        else if (data == 'G')
          num.c[x][y] = 2;
        else if (data == 'B')
          num.c[x][y] = 3;
      }
    }
    num.time = 0;
    queue<state> q;
    q.push(num);
    bool flg1 = true;
    for (int y = 1; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        if (num.c[1][1] != num.c[x][y]) {
          flg1 = false;
          break;
        }
      }
      if (!flg1)
        break;
    }
    if (flg1) {
      cout << num.time << endl;
      continue;
    }
    while (true) {
      state tmp;
      tmp = q.front();
      q.pop();
      for (int i = 1; i <= 3; i++) {
        bool flg = true;
        state copy = tmp;
        if (i == copy.c[1][1])
          continue;
        else {
          DFS(copy, 1, 1, copy.c[1][1], i);
          copy.time++;
          for (int y = 1; y <= Y; y++) {
            for (int x = 1; x <= X; x++) {
              if (copy.c[1][1] != copy.c[x][y]) {
                flg = false;
                break;
              }
            }
            if (!flg)
              break;
          }
          if (flg) {
            cout << copy.time << endl;
            breakflg = true;
            break;
          }
          q.push(copy);
        }
      }
      if (breakflg)
        break;
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
class state {
public:
  int c[12][12];
  int time;
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int X, Y;
state num;
void DFS(state &copy, int x, int y, int oldcolor, int newcolor) {
  if (copy.c[x][y] != oldcolor)
    return;
  else {
    copy.c[x][y] = newcolor;
  }
  for (int i = 0; i < 4; i++) {
    DFS(copy, x + dx[i], y + dy[i], oldcolor, newcolor);
  }
  return;
}
int main() {
  while (true) {
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;
    for (int x = 0; x < 12; x++) {
      for (int y = 0; y < 12; y++) {
        num.c[x][y] == -1;
      }
    }
    for (int y = 1; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        char data;
        cin >> data;
        if (data == 'R')
          num.c[x][y] = 1;
        else if (data == 'G')
          num.c[x][y] = 2;
        else if (data == 'B')
          num.c[x][y] = 3;
      }
    }
    num.time = 0;
    queue<state> q;
    q.push(num);
    while (true) {
      state num;
      num = q.front();
      q.pop();
      bool flg = true;
      for (int y = 1; y <= Y; y++) {
        for (int x = 1; x <= X; x++) {
          if (num.c[1][1] != num.c[x][y]) {
            flg = false;
            break;
          }
        }
        if (!flg)
          break;
      }
      if (flg) {
        cout << num.time << endl;
        break;
      }
      for (int i = 1; i <= 3; i++) {
        state copy = num;
        if (i == copy.c[1][1])
          continue;
        else {
          DFS(copy, 1, 1, copy.c[1][1], i);
          copy.time++;
          q.push(copy);
        }
      }
      if (flg)
        break;
    }
  }
  return 0;
}