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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;

const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int X, Y;
char maps[10][10];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ans;

struct Data {
  int cnt;
  char m[10][10];

  Data(int _cnt, char _m[10][10]) {
    cnt = _cnt;
    REP(i, 10) REP(j, 10) m[i][j] = _m[i][j];
  }
};

int main() {
  while (cin >> X >> Y, X || Y) {
    REP(i, Y) REP(j, X) {
      string s;
      cin >> s;
      int num;
      switch (s[0]) {
      case 'R':
        num = 0;
        break;
      case 'G':
        num = 1;
        break;
      case 'B':
        num = 2;
        break;
      }
      maps[i][j] = num;
    }

    queue<Data> que;
    que.push(Data(0, maps));
    int ans = INF;

    while (!que.empty()) {
      Data now = que.front();
      que.pop();
      if (ans <= now.cnt)
        continue;

      bool en = true;
      FOR(i, 1, Y) REP(j, X) if (now.m[i][j] != now.m[i - 1][j]) en = false;
      FOR(j, 1, X) REP(i, Y) if (now.m[i][j] != now.m[i][j - 1]) en = false;
      if (en) {
        ans = now.cnt;
        continue;
      }

      queue<pii> que2;
      bool done[10][10] = {};
      vector<pii> v[3];

      que2.push(pii(0, 0));
      done[0][0] = true;
      v[now.m[0][0]].push_back(pii(0, 0));

      char base = now.m[0][0];

      while (!que2.empty()) {
        int x = que2.front().se, y = que2.front().fi;
        que2.pop();

        REP(i, 4) {
          int nx = x + dx[i], ny = y + dy[i];
          if (!(nx >= 0 && nx < X && ny >= 0 && ny < Y))
            continue;
          if (done[ny][nx])
            continue;
          if (now.m[ny][nx] == base)
            que2.push(pii(ny, nx));

          done[ny][nx] = true;

          if (now.m[ny][nx] == base ||
              (base != now.m[ny][nx] && v[now.m[ny][nx]].empty()))
            v[now.m[ny][nx]].push_back(pii(ny, nx));
        }
      }

      REP(i, 3) if (i != base) {
        if (v[i].empty())
          continue;

        REP(j, v[base].size()) now.m[v[base][j].fi][v[base][j].se] = i;
        que.push(Data(now.cnt + 1, now.m));
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;

const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int X, Y;
int maps[10][10];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ans;

int dfs(int cnt) {
  if (ans <= cnt)
    return ans;

  bool en = true;
  FOR(i, 1, Y) REP(j, X) if (maps[i][j] != maps[i - 1][j]) en = false;
  FOR(j, 1, X) REP(i, Y) if (maps[i][j] != maps[i][j - 1]) en = false;
  if (en)
    return cnt;

  bool done[10][10] = {};
  queue<pii> que;
  vector<pii> v[3];

  que.push(pii(0, 0));
  done[0][0] = true;
  v[maps[0][0]].push_back(pii(0, 0));

  while (!que.empty()) {
    int x = que.front().se, y = que.front().fi;
    que.pop();

    REP(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (!(nx >= 0 && nx < X && ny >= 0 && ny < Y))
        continue;
      if (done[ny][nx])
        continue;
      if (maps[ny][nx] == maps[y][x])
        que.push(pii(ny, nx));

      done[ny][nx] = true;
      v[maps[ny][nx]].push_back(pii(ny, nx));
    }
  }

  int res = INF;
  int base = maps[0][0];

  REP(i, 3) if (i != base) {
    if (v[i].empty())
      continue;

    REP(j, v[base].size()) maps[v[base][j].fi][v[base][j].se] = i;
    chmin(res, dfs(cnt + 1));
  }
  REP(j, v[base].size()) maps[v[base][j].fi][v[base][j].se] = base;

  return res;
}

int main() {
  while (cin >> X >> Y, X || Y) {
    REP(i, Y) REP(j, X) {
      string s;
      cin >> s;
      int num = -1;
      switch (s[0]) {
      case 'R':
        num = 0;
        break;
      case 'G':
        num = 1;
        break;
      case 'B':
        num = 2;
        break;
      }
      maps[i][j] = num;
    }

    ans = X + Y;
    cout << dfs(0) << endl;
  }
  return 0;
}