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

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int ar[11], ag[11], ab[11];
bool checked[59049];
int p3[11];
map<int, int> change = {{0, 0}, {1, 8}, {2, 4}, {3, 8}, {4, 4},
                        {5, 0}, {6, 4}, {7, 0}, {8, 8}};

int conv(string s) {
  int ret = 0;
  for (int i = 0; i < (int)s.size(); ++i) {
    ret *= 3;
    if (s[i] == 'g')
      ret += 1;
    if (s[i] == 'b')
      ret += 2;
  }
  return ret;
}

int main() {
  p3[0] = 1;
  for (int i = 1; i <= 10; ++i) {
    p3[i] = p3[i - 1] * 3;
  }
  ar[0] = ag[0] = ab[0] = 0;
  for (int i = 1; i <= 10; ++i) {
    ar[i] = ar[i - 1];
    ag[i] = ag[i - 1] * 3 + 1;
    ab[i] = ab[i - 1] * 3 + 2;
  }
  string s;
  while (cin >> s, s != "0") {
    memset(checked, false, sizeof checked);
    int n = s.size();
    int first = conv(s);
    queue<pair<int, int>> q;
    q.push(make_pair(first, 0));
    checked[first] = true;
    int res = -1;
    while (!q.empty()) {
      int col = q.front().first;
      int t = q.front().second;
      q.pop();
      if (col == ar[n] || col == ag[n] || col == ab[n]) {
        res = t;
        break;
      }
      for (int i = 0; i < n - 1; ++i) {
        int replace = change[(col / p3[i]) % 9];
        int nx =
            (col / p3[i + 2]) * p3[i + 2] + replace * p3[i] + (col % p3[i]);
        if (!checked[nx]) {
          q.push(make_pair(nx, t + 1));
          checked[nx] = true;
        }
      }
    }
    if (res < 0) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

map<char, char> rot = {{'r', 'g'}, {'g', 'b'}, {'b', 'r'}};
map<string, bool> used;

void rotate(string &s) {
  for (int i = 0; i < (int)s.size(); ++i)
    s[i] = rot[s[i]];
}

int main() {
  string s;
  while (cin >> s, s != "0") {
    used.clear();
    string r = "", g = "", b = "";
    for (int i = 0; i < (int)s.size(); ++i) {
      r += "r";
      g += "g";
      b += "b";
    }
    queue<pair<string, int>> q;
    q.push(make_pair(s, 0));
    int res = -1;
    while (!q.empty()) {
      string col = q.front().first;
      used[col] = true;
      rotate(col);
      used[col] = true;
      rotate(col);
      used[col] = true;
      rotate(col);
      int t = q.front().second;
      if (col == r || col == g || col == b) {
        res = t;
        break;
      }
      q.pop();
      for (int i = 0; i < (int)col.size() - 1; ++i) {
        if (col[i] != col[i + 1]) {
          string nxt = col;
          if (col[i] != 'r' && col[i + 1] != 'r') {
            nxt[i] = 'r';
            nxt[i + 1] = 'r';
          } else if (col[i] != 'g' && col[i + 1] != 'g') {
            nxt[i] = 'g';
            nxt[i + 1] = 'g';
          } else if (col[i] != 'b' && col[i + 1] != 'b') {
            nxt[i] = 'b';
            nxt[i + 1] = 'b';
          }
          if (!used[nxt]) {
            q.push(make_pair(nxt, t + 1));
          }
        }
      }
    }
    if (res < 0) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}