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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1 << 29;

string col = "RGB";
int w, h;
char v[11][11];

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

int best;

int fill(char col) {
  queue<P> q;
  q.push(MP(0, 0));
  char c = v[0][0];
  int cnt = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (v[p.Y][p.X] != c)
      continue;
    v[p.Y][p.X] = col;
    cnt++;

    rep(d, 4) {
      int nx = p.X + dx[d], ny = p.Y + dy[d];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      q.push(MP(nx, ny));
    }
  }
  return cnt;
}

int visit[11][11];
int count() {
  memset(visit, 0, sizeof(visit));
  queue<P> q;
  q.push(MP(0, 0));
  char c = v[0][0];
  int cnt = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (visit[p.Y][p.X])
      continue;
    visit[p.Y][p.X] = 1;
    cnt++;

    rep(d, 4) {
      int nx = p.X + dx[d], ny = p.Y + dy[d];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (v[ny][nx] == c)
        q.push(MP(nx, ny));
    }
  }
  return cnt;
}

int dfs(int cnt, int depth) {
  if (best <= depth)
    return INF;
  if (cnt == w * h)
    return best = depth;

  int res = INF;
  char c = v[0][0];
  char tmp[11][11];
  memcpy(tmp, v, sizeof(v));
  rep(i, 3) {
    char next = col[(i + depth) % 3];
    if (c == next)
      continue;
    fill(next);
    int ncnt = count();
    if (cnt != ncnt)
      res = min(res, dfs(ncnt, depth + 1));
    memcpy(v, tmp, sizeof(v));
  }
  return res;
}

int main(void) {
  while (cin >> w >> h, w | h) {
    best = w + h;
    rep(i, h) rep(j, w) cin >> v[i][j];
    cout << dfs(count(), 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1 << 29;

string col = "RGB";
int w, h;
char v[11][11];

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

int dfs(int cnt) {
  if (cnt == w * h)
    return -1;
  int res = INF;
  rep(i, 3) {
    if (v[0][0] == col[i])
      continue;
    char tmp[11][11];
    memcpy(tmp, v, sizeof(v));

    queue<P> q;
    q.push(MP(0, 0));
    char c = v[0][0];
    int num = 0;
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      if (v[p.Y][p.X] != c)
        continue;
      v[p.Y][p.X] = col[i];
      num++;

      rep(d, 4) {
        int nx = p.X + dx[d], ny = p.Y + dy[d];
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        q.push(MP(nx, ny));
      }
    }
    if (cnt == num)
      return INF;
    res = min(res, dfs(num));
    memcpy(v, tmp, sizeof(v));
  }
  return res + 1;
}

int main(void) {
  while (cin >> w >> h, w | h) {
    rep(i, h) rep(j, w) cin >> v[i][j];
    cout << dfs(0) << endl;
  }

  return 0;
}