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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

struct aa {
  vector<vector<char>> field;
  int time;
};

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int W, H;
vector<vector<char>> ch_color(vector<vector<char>> &field, int nextco) {
  queue<pair<int, int>> que;
  vector<vector<char>> nextfield(field);
  que.push(make_pair(0, 0));
  vector<vector<int>> memo(H, vector<int>(W));
  int fromco = field[0][0];
  nextfield[0][0] = nextco;
  while (!que.empty()) {
    auto p(que.front());
    que.pop();
    for (int way = 0; way < 4; ++way) {
      const int nextx = p.first + dx[way];
      const int nexty = p.second + dy[way];
      if (nextx >= 0 && nextx < W && nexty >= 0 && nexty < H) {
        if (memo[nexty][nextx])
          continue;
        else {
          memo[nexty][nextx] = true;
          if (field[nexty][nextx] == fromco) {
            nextfield[nexty][nextx] = nextco;
            que.push(make_pair(nextx, nexty));
          } else {
          }
        }
      }
    }
  }
  return nextfield;
}

int main() {
  while (1) {
    cin >> W >> H;
    if (!W)
      break;
    queue<aa> que;
    set<vector<vector<char>>> aset;
    {
      vector<vector<char>> field(H, vector<char>(W));
      for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
          char c;
          cin >> c;
          if (c == 'R')
            field[i][j] = 0;
          else if (c == 'B')
            field[i][j] = 1;
          else
            field[i][j] = 2;
        }
      }
      que.push(aa{field, 0});
      aset.emplace(field);
    }

    int ans;
    while (!que.empty()) {
      aa atop(que.front());
      auto afield(atop.field);
      bool ok = true;
      for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
          if (atop.field[i][j] != atop.field[0][0])
            ok = false;
        }
      }
      if (ok) {
        ans = atop.time;
        break;
      }
      que.pop();
      for (int co = 0; co < 3; ++co) {
        if (co == afield[0][0])
          continue;
        else {
          vector<vector<char>> nextfield(ch_color(afield, co));
          if (aset.find(nextfield) == aset.end()) {
            aset.emplace(nextfield);
            que.push(aa{nextfield, atop.time + 1});
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

struct aa {
  vector<vector<char>> field;
  int time;
};

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int W, H;
vector<vector<char>> ch_color(vector<vector<char>> &field, int nextco) {
  queue<pair<int, int>> que;
  vector<vector<char>> nextfield(field);
  que.push(make_pair(0, 0));
  vector<vector<int>> memo(H, vector<int>(W));
  int fromco = field[0][0];
  nextfield[0][0] = nextco;
  while (!que.empty()) {
    auto p(que.front());
    que.pop();
    for (int way = 0; way < 4; ++way) {
      const int nextx = p.first + dx[way];
      const int nexty = p.second + dy[way];
      if (nextx >= 0 && nextx < W && nexty >= 0 && nexty < H) {
        if (memo[nexty][nextx])
          continue;
        else {
          memo[nexty][nextx] = true;
          if (field[nexty][nextx] == fromco) {
            nextfield[nexty][nextx] = nextco;
            que.push(make_pair(nextx, nexty));
          } else {
          }
        }
      }
    }
  }
  return nextfield;
}

int main() {
  while (1) {
    cin >> W >> H;
    if (!W)
      break;
    queue<aa> que;
    {
      vector<vector<char>> field(H, vector<char>(W));
      for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
          char c;
          cin >> c;
          if (c == 'R')
            field[i][j] = 0;
          else if (c == 'B')
            field[i][j] = 1;
          else
            field[i][j] = 2;
        }
      }
      que.push(aa{field, 0});
    }

    int ans;
    while (!que.empty()) {
      aa atop(que.front());
      auto afield(atop.field);
      bool ok = true;
      for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
          if (atop.field[i][j] != atop.field[0][0])
            ok = false;
        }
      }
      if (ok) {
        ans = atop.time;
        break;
      }
      que.pop();
      for (int co = 0; co < 3; ++co) {
        if (co == afield[0][0])
          continue;
        else {
          vector<vector<char>> nextfield(ch_color(afield, co));
          que.push(aa{nextfield, atop.time + 1});
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}