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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int MAX_Y = 11;
const int MAX_X = 11;
const int INF = 1e9 + 7;

int Y, X, ans;
int my[] = {0, 1, 0, -1};
int mx[] = {1, 0, -1, 0};

void change_color(int y, int x, int color, int bef_color, int v[MAX_Y][MAX_X]) {
  // cout <<y <<", " <<x <<endl;
  v[y][x] = color;
  REP(i, 4) {
    int ny = y + my[i], nx = x + mx[i];
    if (ny >= 0 && nx >= 0 && ny < Y && nx < X && v[ny][nx] == bef_color)
      change_color(ny, nx, color, bef_color, v);
  }
}

bool isOK(int v[MAX_Y][MAX_X]) {
  REP(i, Y) REP(j, X) if (v[i][j] != v[0][0]) return false;
  return true;
}

int cntColor(int y, int x, int v[MAX_Y][MAX_X], int visited[MAX_Y][MAX_X]) {
  int ret = 1;
  visited[y][x] = true;
  REP(i, 2) {
    int ny = y + my[i], nx = x + mx[i];
    if (ny >= 0 && nx >= 0 && ny < Y && nx < X && v[ny][nx] == v[0][0] &&
        !visited[ny][nx])
      ret += cntColor(ny, nx, v, visited);
  }
  return ret;
}

void dfs(int cnt, int all_cnt, int v[MAX_Y][MAX_X]) {
  // cout <<"--------" <<endl;
  // cout <<cnt <<", " <<isOK(v) <<endl;
  // REP(i, Y){
  //   REP(j, X) cout <<v[i][j] <<" ";
  //   cout <<endl;
  // }
  if (isOK(v))
    ans = cnt;
  if (cnt >= ans)
    return;
  REP(i, 3) {
    if (v[0][0] == i)
      continue;
    // cout <<i <<"->" <<endl;
    int tmp[MAX_Y][MAX_X];
    REP(i, Y) memcpy(tmp[i], v[i], sizeof(v[i]));
    change_color(0, 0, i, v[0][0], tmp);
    int tmptmp[MAX_X][MAX_Y] = {0};
    int c = cntColor(0, 0, tmp, tmptmp);
    // REP(i, Y){
    //   REP(j, X) cout <<tmp[i][j] <<" ";
    //   cout <<endl;
    // }
    if (c > all_cnt)
      dfs(cnt + 1, c, tmp);
  }
}

int main() {
  int v[MAX_Y][MAX_X];
  while (cin >> X >> Y && Y) {
    ans = INF;
    REP(i, Y) {
      REP(j, X) {
        char tmp;
        cin >> tmp;
        if (tmp == 'R')
          v[i][j] = 0;
        if (tmp == 'G')
          v[i][j] = 1;
        if (tmp == 'B')
          v[i][j] = 2;
      }
    }
    int tmp[MAX_X][MAX_Y] = {0};
    REP(i, 3) dfs(0, cntColor(0, 0, v, tmp), v);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int MAX_Y = 11;
const int MAX_X = 11;

int Y, X;

class C {
public:
  int ans, cnt;
  vector<vector<int>> v;
  C(int _ans, int _cnt, vector<vector<int>> _v) {
    ans = _ans;
    cnt = _cnt;
    v = _v;
  }
  C() {}
};

bool isOK(vector<vector<int>> &v) {
  REP(y, Y) REP(x, X) if (v[y][x] != v[0][0]) return false;
  return true;
}

void dfs(vector<vector<int>> &v, int y, int x, int bef, int aft) {
  v[y][x] = aft;
  int my[] = {0, 0, 1, -1};
  int mx[] = {1, -1, 0, 0};
  REP(i, 4) {
    int ny = y + my[i];
    int nx = x + mx[i];
    if (ny < 0 || nx < 0 || ny >= Y || nx >= X || v[ny][nx] != bef)
      continue;
    dfs(v, ny, nx, bef, aft);
  }
}

int cnt_panel(vector<vector<int>> v, int y, int x, int bef) {
  int ret = 1;
  int my[] = {0, 0, 1, -1};
  int mx[] = {1, -1, 0, 0};
  v[y][x] = -1;
  REP(i, 4) {
    int ny = y + my[i];
    int nx = x + mx[i];
    if (ny < 0 || nx < 0 || ny >= Y || nx >= X || v[ny][nx] != bef)
      continue;
    ret += cnt_panel(v, ny, nx, bef);
  }
  return ret;
}

int calc() {
  vector<vector<int>> v(Y, vector<int>(X));
  REP(y, Y) {
    REP(x, X) {
      char c;
      cin >> c;
      if (c == 'R')
        v[y][x] = 0;
      if (c == 'G')
        v[y][x] = 1;
      if (c == 'B')
        v[y][x] = 2;
    }
  }
  queue<C> open;
  open.push(C(0, cnt_panel(v, 0, 0, v[0][0]), v));
  while (!open.empty()) {
    C tmp = open.front();
    open.pop();
    int cnt = tmp.cnt, ans = tmp.ans;
    vector<vector<int>> v = tmp.v;
    // debug
    // cout <<"-----------" <<endl;
    // cout <<cnt <<endl;
    // REP(i, Y){
    //   REP(j, X) cout <<v[i][j] <<" ";
    //   cout <<endl;
    // }
    if (isOK(v))
      return ans;
    REP(next, 3) {
      if (v[0][0] == next)
        continue;
      vector<vector<int>> tmp_v = v;
      dfs(tmp_v, 0, 0, tmp_v[0][0], next);
      int tmp_cnt = cnt_panel(tmp_v, 0, 0, tmp_v[0][0]);
      if (tmp_cnt == cnt)
        continue;
      open.push(C(ans + 1, tmp_cnt, tmp_v));
    }
  }
  return -1;
}

int main() {
  while (cin >> X >> Y && X)
    cout << calc() << endl;
  return 0;
}