Problem p00243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
int h, w, n, ans, colc;
char cmap[110][110];
bool used[110][110];
static const int dx[4] = {0, 1, 0, -1};
static const int dy[4] = {1, 0, -1, 0};

void display() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cout << cmap[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

void draw(char c, char t, int x, int y) {
  cmap[y][x] = t;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && c == cmap[ny][nx]) {
      draw(c, t, nx, ny);
    }
  }
}

bool fin() {
  bool finish = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (cmap[0][0] != cmap[i][j])
        return false;
    }
  }
  return true;
}

int search(int x, int y) {
  used[y][x] = true;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && cmap[0][0] == cmap[ny][nx] &&
        !used[ny][nx]) {
      colc++;
      search(nx, ny);
    }
  }
}

void rec(int cnt) {
  if (fin()) {
    ans = min(ans, cnt);
    return;
  }
  // display();
  char A, B, erase[110][110];
  if (cmap[0][0] == 'R') {
    A = 'B';
    B = 'G';
  } else if (cmap[0][0] == 'B') {
    A = 'R';
    B = 'G';
  } else if (cmap[0][0] == 'G') {
    A = 'B';
    B = 'R';
  }
  colc = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  search(0, 0);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      erase[i][j] = cmap[i][j];
  draw(cmap[0][0], A, 0, 0);
  int precolc = colc;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  colc = 0;
  search(0, 0);
  if (colc != precolc)
    rec(cnt + 1);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cmap[i][j] = erase[i][j];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  colc = 0;
  search(0, 0);
  draw(cmap[0][0], B, 0, 0);
  precolc = colc;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  colc = 0;
  search(0, 0);
  if (colc != precolc)
    rec(cnt + 1);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cmap[i][j] = erase[i][j];
}

int main() {

  while (true) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    ans = (1 << 29);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> cmap[i][j];
      }
    }

    rec(0);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
int h, w, n, ans, colc;
char cmap[110][110];
bool used[110][110];
static const int dx[4] = {0, 1, 0, -1};
static const int dy[4] = {1, 0, -1, 0};

void display() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cout << cmap[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

void draw(char c, char t, int x, int y) {
  cmap[y][x] = t;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && c == cmap[ny][nx]) {
      draw(c, t, nx, ny);
    }
  }
}

bool fin() {
  bool finish = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (cmap[0][0] != cmap[i][j])
        return false;
    }
  }
  return true;
}

int search(int x, int y) {
  used[y][x] = true;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && cmap[0][0] == cmap[ny][nx] &&
        !used[ny][nx]) {
      colc++;
      search(nx, ny);
    }
  }
}

void rec(int cnt) {
  if (cnt >= ans)
    return;
  if (fin()) {
    ans = min(ans, cnt);
    return;
  }
  // display();
  char A, B, erase[110][110];
  if (cmap[0][0] == 'R') {
    A = 'B';
    B = 'G';
  } else if (cmap[0][0] == 'B') {
    A = 'R';
    B = 'G';
  } else if (cmap[0][0] == 'G') {
    A = 'B';
    B = 'R';
  }
  colc = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  search(0, 0);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      erase[i][j] = cmap[i][j];
  draw(cmap[0][0], A, 0, 0);
  int precolc = colc;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  colc = 0;
  search(0, 0);
  if (colc != precolc)
    rec(cnt + 1);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cmap[i][j] = erase[i][j];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  colc = 0;
  search(0, 0);
  draw(cmap[0][0], B, 0, 0);
  precolc = colc;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      used[i][j] = false;
  colc = 0;
  search(0, 0);
  if (colc != precolc)
    rec(cnt + 1);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cmap[i][j] = erase[i][j];
}

int main() {

  while (true) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    ans = (1 << 29);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> cmap[i][j];
      }
    }

    rec(0);
    cout << ans << endl;
  }
  return 0;
}