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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<char>> F;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
struct Fi {
  int turn;
  char f[10][10];
  Fi(int t, char b[10][10]) {
    turn = t;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        f[i][j] = b[i][j];
      }
    }
  }
};
int n, m;
char rgb[4] = "RGB";
bool All(char a[10][10]) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] != a[0][0])
        return false;
    }
  }
  return true;
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char buf[10][10];
char st[10][10];
bool used[10][10];
char sor;
int ty;
void print(char a[10][10]) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  return;
}
void dfs(int x, int y) {
  // cout << x << ' ' << y << endl;
  used[x][y] = true;
  buf[x][y] = rgb[ty];
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= n || ny < 0 || ny >= m)
      continue;
    if (used[nx][ny])
      continue;
    if (buf[nx][ny] != sor)
      continue;
    dfs(nx, ny);
  }
  return;
}
int bfs() {
  if (All(st))
    return 0;
  queue<Fi> q;
  Fi start(0, st);
  q.push(start);
  while (!q.empty()) {
    Fi a = q.front();
    // print(a.f);
    q.pop();
    for (int i = 0; i < 3; i++) {
      if ((a.f)[0][0] == rgb[i])
        continue;
      memset(used, false, sizeof(used));
      sor = (a.f)[0][0];
      ty = i;
      memcpy(buf, a.f, sizeof(a.f));
      dfs(0, 0);
      // print(nb);
      // cout << a.fi+1 << endl << endl;
      if (All(buf))
        return a.turn + 1;
      q.push(Fi(a.turn + 1, buf));
    }
  }
  return 0;
}
int main() {
  while (1) {
    cin >> m >> n;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> st[i][j];
      }
    }
    cout << bfs() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<char>> F;
typedef pair<int, F> Fi;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
int n, m;
int ans = 100;
char rgb[4] = "RGB";
bool All(F a) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] != a[0][0])
        return false;
    }
  }
  return true;
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
F buf;
bool used[10][10];
char sor;
int ty;
void print(F a) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  return;
}
void dfs(int x, int y) {
  // cout << x << ' ' << y << endl;
  used[x][y] = true;
  buf[x][y] = rgb[ty];
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= n || ny < 0 || ny >= m)
      continue;
    if (used[nx][ny])
      continue;
    if (buf[nx][ny] != sor)
      continue;
    dfs(nx, ny);
  }
  return;
}
F change(F b, int x) {
  memset(used, false, sizeof(used));
  sor = b[0][0];
  ty = x;
  buf = b;
  dfs(0, 0);
  return buf;
}
int bfs(F st) {
  if (All(st))
    return 0;
  queue<Fi> q;
  q.push(Fi(0, st));
  while (!q.empty()) {
    Fi a = q.front();
    q.pop();
    F b = a.sec;
    for (int i = 0; i < 3; i++) {
      if (b[0][0] == rgb[i])
        continue;
      memset(used, false, sizeof(used));
      sor = b[0][0];
      ty = i;
      buf = b;
      dfs(0, 0);
      // print(nb);
      // cout << a.fi+1 << endl << endl;
      if (All(buf))
        return a.fi + 1;
      q.push(Fi(a.fi + 1, buf));
    }
  }
  return 0;
}
int main() {
  while (1) {
    cin >> m >> n;
    if (n == 0 && m == 0)
      break;
    F st(n, vector<char>(m));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> st[i][j];
      }
    }
    cout << bfs(st) << endl;
  }
  return 0;
}