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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-10
#define PB push_back
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
typedef pair<int, int> Pii;
typedef vector<int> vi;
class Data {
public:
  char field[10][10];
  int cnt;
  Data() {}
  Data(char _field[10][10], int _cnt) {
    cnt = _cnt;
    rep(i, 10) {
      rep(j, 10) { field[i][j] = _field[i][j]; }
    }
  }
};
bool check(char stage[10][10], int w, int h) {
  int top = stage[0][0];
  rep(i, h) {
    rep(j, w) {
      if (top != stage[i][j])
        return false;
    }
  }
  return true;
}
void copy(char sub[2][10][10], char stage[10][10], int w, int h) {
  rep(i, h) {
    rep(j, w) {
      sub[0][i][j] = stage[i][j];
      sub[1][i][j] = stage[i][j];
    }
  }
  return;
}
void dfs(int y, int x, bool memo[10][10], int w, int h, char stage[2][10][10],
         int col) {
  rep(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx > w - 1 || ny < 0 || ny > h - 1 || memo[ny][nx] == 1)
      continue;
    if (col == stage[0][ny][nx] || col == stage[1][ny][nx]) {
      stage[0][ny][nx] = (col + 1) % 3;
      stage[1][ny][nx] = (col + 2) % 3;
      memo[ny][nx] = 1;
      dfs(ny, nx, memo, w, h, stage, col);
    }
  }
  return;
}
int bfs(char stage[10][10], int w, int h) {
  int ans = -1;
  queue<Data> Q;
  Q.push(Data(stage, 0));
  while (!Q.empty()) {
    Data d = Q.front();
    Q.pop();
    if (check(d.field, w, h)) {
      ans = d.cnt;
      break;
    }
    bool memo[10][10] = {{0}};
    char sub[2][10][10] = {{0}};
    copy(sub, d.field, w, h);
    memo[0][0] = 1;
    sub[0][0][0] = (d.field[0][0] + 1) % 3;
    sub[1][0][0] = (d.field[0][0] + 2) % 3;
    dfs(0, 0, memo, w, h, sub, d.field[0][0]);
    Q.push(Data(sub[0], d.cnt + 1));
    Q.push(Data(sub[1], d.cnt + 1));
  }
  return ans;
}
int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    char stage[10][10];
    rep(i, h) {
      rep(j, w) {
        char tmp;
        scanf(" %c", &tmp);
        stage[i][j] = ((tmp == 'R') ? 0 : (tmp == 'G') ? 1 : 2);
      }
    }
    int ans = bfs(stage, w, h);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-10
#define PB push_back
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
typedef pair<int, int> Pii;
typedef vector<int> vi;
class Data {
public:
  int field[10][10];
  int cnt;
  Data() {}
  Data(int _field[10][10], int _cnt) {
    cnt = _cnt;
    rep(i, 10) {
      rep(j, 10) { field[i][j] = _field[i][j]; }
    }
  }
};
bool check(int stage[10][10], int w, int h) {
  int top = stage[0][0];
  rep(i, h) {
    rep(j, w) {
      if (top != stage[i][j])
        return false;
    }
  }
  return true;
}
void copy(int sub[2][10][10], int stage[10][10], int w, int h) {
  rep(i, h) {
    rep(j, w) {
      sub[0][i][j] = stage[i][j];
      sub[1][i][j] = stage[i][j];
    }
  }
  return;
}
void dfs(int y, int x, bool memo[10][10], int w, int h, int stage[2][10][10],
         int col) {
  rep(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx > w - 1 || ny < 0 || ny > h - 1 || memo[ny][nx] == 1)
      continue;
    if (col == stage[0][ny][nx] || col == stage[1][ny][nx]) {
      stage[0][ny][nx] = (col + 1) % 3;
      stage[1][ny][nx] = (col + 2) % 3;
      memo[ny][nx] = 1;
      dfs(ny, nx, memo, w, h, stage, col);
    }
  }
  return;
}
int bfs(int stage[10][10], int w, int h) {
  int ans = -1;
  queue<Data> Q;
  Q.push(Data(stage, 0));
  while (!Q.empty()) {
    Data d = Q.front();
    Q.pop();
    if (check(d.field, w, h)) {
      ans = d.cnt;
      break;
    }
    bool memo[10][10] = {{0}};
    int sub[2][10][10] = {{0}};
    copy(sub, d.field, w, h);
    memo[0][0] = 1;
    sub[0][0][0] = (d.field[0][0] + 1) % 3;
    sub[1][0][0] = (d.field[0][0] + 2) % 3;
    dfs(0, 0, memo, w, h, sub, d.field[0][0]);
    Q.push(Data(sub[0], d.cnt + 1));
    Q.push(Data(sub[1], d.cnt + 1));
  }
  return ans;
}
int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    int stage[10][10];
    rep(i, h) {
      rep(j, w) {
        char tmp;
        scanf(" %c", &tmp);
        stage[i][j] = ((tmp == 'R') ? 0 : (tmp == 'G') ? 1 : 2);
      }
    }
    int ans = bfs(stage, w, h);
    printf("%d\\n", ans);
  }
  return 0;
}