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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int h, w;

// ll encode(int a[10][10]) {
//   ll res = 0;
//   REP(i,h)REP(j,w) res = res*3+a[i][j];
//   return res;
// }
// void decode(int a[10][10], ll e) {
//   for (int i=h-1; i>=0; --i) {
//     for (int j=w-1; j>=0; --j) {
//       a[i][j] = e%3;
//       e /= 3;
//     }
//   }
// }
typedef vector<int> vec;
typedef vector<vec> mat;

// int ba[10][10];
// int now[10][10];
// int nxt[10][10];

bool visited[10][10];

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};

int ans;
void dfs(mat now, int d) {
  // cout << d << endl;
  // REP(i,h) {
  //   REP(j,w) cout << now[i][j];
  //   cout << endl;
  // }

  int col = now[0][0];
  queue<pii> Q;
  Q.push(pii(0, 0));
  REP(i, h) REP(j, w) visited[i][j] = 0;
  visited[0][0] = 1;
  vector<pii> ps;
  bool ncol[3] = {};
  while (!Q.empty()) {
    pii p = Q.front();
    Q.pop();
    ps.push_back(p);
    REP(i, 4) {
      int y = p.first + dy[i];
      int x = p.second + dx[i];
      if (y < 0 || y >= h || x < 0 || x >= w)
        continue;
      if (now[y][x] != col) {
        ncol[now[y][x]] = 1;
        continue;
      }
      if (!visited[y][x]) {
        visited[y][x] = 1;
        Q.push(pii(y, x));
      }
    }
  }
  if (ps.size() == h * w) {
    ans = min(ans, d);
    return;
  }
  if (d == ans - 1)
    return;
  REP(k, 3) {
    if (ncol[k] == 0)
      continue;
    FOR(it, ps) { now[it->first][it->second] = k; }
    dfs(now, d + 1);
  }
}

int main() {
  while (cin >> w >> h, h || w) {
    mat ba(h, vec(w));
    REP(i, h) {
      REP(j, w) {
        char c;
        cin >> c;
        if (c == 'R')
          ba[i][j] = 0;
        else if (c == 'G')
          ba[i][j] = 1;
        else
          ba[i][j] = 2;
      }
    }
    ans = INF;
    dfs(ba, 0);
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int h, w;

ll encode(int a[20][20]) {
  ll res = 0;
  REP(i, h) REP(j, w) res = res * 3 + a[i][j];
  return res;
}
void decode(int a[20][20], ll e) {
  for (int i = h - 1; i >= 0; --i) {
    for (int j = w - 1; j >= 0; --j) {
      a[i][j] = e % 3;
      e /= 3;
    }
  }
}

int ba[20][20];
int now[20][20];
int nxt[20][20];

bool visited[20][20];

int main() {
  while (cin >> w >> h, h || w) {
    REP(i, h) {
      REP(j, w) {
        char c;
        cin >> c;
        if (c == 'R')
          ba[i][j] = 0;
        else if (c == 'G')
          ba[i][j] = 1;
        else
          ba[i][j] = 2;
      }
    }
    queue<ll> Q;
    map<ll, int> mp;
    ll e = encode(ba);
    Q.push(e);
    mp[e] = 0;
    int ans = -1;
    while (!Q.empty()) {
      ll p = Q.front();
      Q.pop();
      ll d = mp[p];
      decode(now, p);
      // cout << d << endl;
      // REP(i,h) {REP(j,w)cout << now[i][j];cout << endl;}
      REP(k, 3) {
        if (now[0][0] == k)
          continue;
        REP(i, h) REP(j, w) nxt[i][j] = now[i][j];
        queue<pii> Q2;
        Q2.push(pii(0, 0));
        REP(i, h) REP(j, w) visited[i][j] = 0;
        visited[0][0] = 1;
        int cnt = 0;
        while (!Q2.empty()) {
          pii p = Q2.front();
          Q2.pop();
          nxt[p.first][p.second] = k;
          ++cnt;
          REP(i, 2) {
            int y = p.first;
            int x = p.second;
            if (i)
              x++;
            else
              y++;
            if (y >= h || x >= w)
              continue;
            if (now[y][x] != now[0][0])
              continue;
            if (!visited[y][x]) {
              visited[y][x] = 1;
              Q2.push(pii(y, x));
            }
          }
        }
        if (cnt == h * w) {
          ans = d;
          goto end;
        }
        ll e = encode(nxt);
        if (mp.count(e) == 0) {
          mp[e] = d + 1;
          Q.push(e);
        }
      }
    }
  end:;
    cout << ans << endl;
  }
}