Problem p00243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int MAX_W = 10;

int w, h, ans;
bool memo[MAX_W][MAX_W];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char C[3] = {'R', 'G', 'B'};
int c_to_n[256] = {0};

void debug(vector<string> &v, vector<string> &v_, int cnt = 0) {
  cout << "[debug] cnt:" << cnt - 1 << " -> " << cnt << endl;
  cout << "before:" << endl;
  for (int y = 0; y < h; y++) {
    cout << v[y] << endl;
  }
  cout << "after:" << endl;
  for (int y = 0; y < h; y++) {
    cout << v_[y] << endl;
  }
  cout << endl;
}

bool check(vector<string> &v, char c) {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (v[y][x] != c) {
        return false;
      }
    }
  }
  return true;
}

void dfs(int x, int y, char c, vector<string> v, vector<P> &vp) {
  memo[y][x] = true;
  if (v[y][x] == c) {
    vp.push_back(P(x, y));
  }

  for (int i = 0; i < 4; i++) {
    int mx = x + dx[i];
    int my = y + dy[i];
    if (mx < 0 || my < 0 || w <= mx || h <= my)
      continue;
    if (!memo[my][mx] && v[my][mx] == c) {
      dfs(mx, my, c, v, vp);
    }
  }
}

void solve(vector<string> &v, int cnt) {
  if (ans < cnt)
    return;
  if (check(v, v[0][0])) {
    ans = min(ans, cnt);
    return;
  }
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      memo[y][x] = false;
    }
  }
  vector<P> vp;
  dfs(0, 0, v[0][0], v, vp);
  bool color[3] = {false};
  for (int i = 0; i < vp.size(); i++) {
    for (int j = 0; j < 4; j++) {
      int mx = vp[i].first + dx[j];
      int my = vp[i].second + dy[j];
      if (mx < 0 || my < 0 || w <= mx || h <= my)
        continue;
      if (v[0][0] == v[my][mx])
        continue;
      color[c_to_n[v[my][mx]]] = true;

      if ((color[0] && color[1]) || (color[0] && color[2]) ||
          (color[1] && color[2])) {
        break;
      }
    }
    if ((color[0] && color[1]) || (color[0] && color[2]) ||
        (color[1] && color[2])) {
      break;
    }
  }

  for (int i = 0; i < 3; i++) {
    if (!color[i])
      continue;
    // if( v[0][0] == C[i] ) continue;

    vector<string> next = v;
    for (int j = 0; j < vp.size(); j++) {
      next[vp[j].second][vp[j].first] = C[i];
    }
    if (cnt + 1 <= ans) {
      // debug(v,next,cnt+1);
      solve(next, cnt + 1);
    }
  }
}

int main() {
  c_to_n['R'] = 0;
  c_to_n['G'] = 1;
  c_to_n['B'] = 2;
  while (cin >> w >> h, w || h) {
    vector<string> v(h);
    for (int y = 0; y < h; y++) {
      v[y] = string(w, ' ');
      for (int x = 0; x < w; x++) {
        cin >> v[y][x];
      }
    }
    ans = min(w * h - 1, 17);
    solve(v, 0);
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int MAX_W = 10;

int w, h, ans;
bool memo[MAX_W][MAX_W];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char C[3] = {'R', 'G', 'B'};
int c_to_n[256] = {0};

void debug(vector<string> &v, vector<string> &v_, int cnt = 0) {
  cout << "[debug] cnt:" << cnt - 1 << " -> " << cnt << endl;
  cout << "before:" << endl;
  for (int y = 0; y < h; y++) {
    cout << v[y] << endl;
  }
  cout << "after:" << endl;
  for (int y = 0; y < h; y++) {
    cout << v_[y] << endl;
  }
  cout << endl;
}

bool check(vector<string> &v, char c) {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (v[y][x] != c) {
        return false;
      }
    }
  }
  return true;
}

void dfs(int x, int y, char c, vector<string> &v, vector<P> &vp) {
  memo[y][x] = true;
  if (v[y][x] == c) {
    vp.push_back(P(x, y));
  }

  for (int i = 0; i < 4; i++) {
    int mx = x + dx[i];
    int my = y + dy[i];
    if (mx < 0 || my < 0 || w <= mx || h <= my)
      continue;
    if (!memo[my][mx] && v[my][mx] == c) {
      dfs(mx, my, c, v, vp);
    }
  }
}

void solve(vector<string> &v, int cnt) {
  if (ans < cnt)
    return;
  if (check(v, v[0][0])) {
    ans = min(ans, cnt);
    return;
  }
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      memo[y][x] = false;
    }
  }
  vector<P> vp;
  dfs(0, 0, v[0][0], v, vp);
  bool color[3] = {false};
  for (int i = 0; i < vp.size(); i++) {
    for (int j = 0; j < 4; j++) {
      int mx = vp[i].first + dx[j];
      int my = vp[i].second + dy[j];
      if (mx < 0 || my < 0 || w <= mx || h <= my)
        continue;
      if (v[0][0] == v[my][mx])
        continue;
      color[c_to_n[v[my][mx]]] = true;

      if ((color[0] && color[1]) || (color[0] && color[2]) ||
          (color[1] && color[2])) {
        break;
      }
    }
    if ((color[0] && color[1]) || (color[0] && color[2]) ||
        (color[1] && color[2])) {
      break;
    }
  }

  for (int i = 0; i < 3; i++) {
    if (!color[i])
      continue;
    // if( v[0][0] == C[i] ) continue;

    vector<string> next = v;
    for (int j = 0; j < vp.size(); j++) {
      next[vp[j].second][vp[j].first] = C[i];
    }
    if (cnt + 1 <= ans) {
      // debug(v,next,cnt+1);
      solve(next, cnt + 1);
    }
  }
}

int main() {
  c_to_n['R'] = 0;
  c_to_n['G'] = 1;
  c_to_n['B'] = 2;
  while (cin >> w >> h, w || h) {
    vector<string> v(h);
    for (int y = 0; y < h; y++) {
      v[y] = string(w, ' ');
      for (int x = 0; x < w; x++) {
        cin >> v[y][x];
      }
    }
    ans = min(w * h - 1, 17);
    solve(v, 0);
    cout << ans << endl;
  }
}