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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<char> vc;
typedef vector<vc> vvc;

char grid[10][10];
int h, w;

void fill(char from, char to, int i, int j) {
  grid[i][j] = to;
  rep(k, 4) {
    int ni = i + "\\xff\\x1\\0\\0"[k], nj = j + "\\0\\0\\xff\\x1"[k];
    if (0 <= ni && ni < h && 0 <= nj && nj < w && grid[ni][nj] == from)
      fill(from, to, ni, nj);
  }
}

bool ok() {
  rep(i, h) rep(j, w) if (grid[i][j] != grid[0][0]) return false;
  return true;
}

bool iddfs(int d, int dmax) {
  if (d > dmax)
    return false;
  if (ok())
    return true;

  char g[10][10];
  rep(i, h) rep(j, w) g[i][j] = grid[i][j];

  rep(k, 3) {
    char to = "RGB"[k];
    if (to == grid[0][0])
      continue;
    fill(g[0][0], to, 0, 0);
    if (iddfs(d + 1, dmax))
      return true;

    rep(i, h) rep(j, w) grid[i][j] = g[i][j];
  }
  return false;
}

int solve() {
  for (int i = 0;; i++)
    if (iddfs(0, i))
      return i;
}

int main() {
  while (cin >> w >> h, w | h) {
    rep(i, h) rep(j, w) cin >> grid[i][j];
    cout << solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<char> vc;
typedef vector<vc> vvc;

char grid[10][10];
int h, w;

void fill(char from, char to, int i, int j) {
  if (i < 0 || h <= i || j < 0 || w <= j || grid[i][j] != from)
    return;
  grid[i][j] = to;
  fill(from, to, i - 1, j);
  fill(from, to, i + 1, j);
  fill(from, to, i, j - 1);
  fill(from, to, i, j + 1);
}

bool ok() {
  rep(i, h) rep(j, w) if (grid[i][j] != grid[0][0]) return false;
  return true;
}

bool iddfs(int d, int dmax) {
  if (d > dmax)
    return false;
  if (ok())
    return true;

  char g[10][10];
  rep(i, h) rep(j, w) g[i][j] = grid[i][j];

  rep(k, 3) {
    char to = "RGB"[k];
    if (to == grid[0][0])
      continue;
    fill(g[0][0], to, 0, 0);
    if (iddfs(d + 1, dmax))
      return true;

    rep(i, h) rep(j, w) grid[i][j] = g[i][j];
  }
  return false;
}

int solve() {
  for (int i = 0;; i++)
    if (iddfs(0, i))
      return i;
}

int main() {
  while (cin >> w >> h, w | h) {
    rep(i, h) rep(j, w) cin >> grid[i][j];
    cout << solve() << endl;
  }

  return 0;
}