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

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int X, Y;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int res;
int closed[16][16];

void filling(int s[16][16], int y, int x, int from, int to) {
  s[y][x] = to;
  rep(d, 4) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= Y || nx < 0 || nx >= X || s[ny][nx] != from)
      continue;
    filling(s, ny, nx, from, to);
  }
  return;
}

int check(int s[16][16]) {
  int p = s[0][0];
  rep(i, Y) rep(j, X) {
    if (s[i][j] != p)
      return 0;
  }
  return 1;
}

void next(int from[16][16], int to1[16][16], int to2[16][16]) {
  int c1, c2;
  c1 = c2 = from[0][0];
  c1 += 1;
  c2 += 2;
  c1 %= 3;
  c2 %= 3;

  rep(i, Y) rep(j, X) { to1[i][j] = to2[i][j] = from[i][j]; }

  queue<pi> open;
  open.push(pi(0, 0));
  memset(closed, 0, sizeof(closed));
  to1[0][0] = c1;
  to2[0][0] = c2;

  while (!open.empty()) {
    pi p = open.front();
    open.pop();

    rep(d, 4) {
      int ny = p.F + dy[d];
      int nx = p.S + dx[d];

      if (ny < 0 || nx < 0 || ny >= Y || nx >= X || closed[ny][nx] ||
          from[0][0] != from[ny][nx])
        continue;
      to1[ny][nx] = c1;
      to2[ny][nx] = c2;
      closed[ny][nx] = 1;
      open.push(pi(ny, nx));
    }
  }
}

void dfs(int s[16][16], int n) {
  if (check(s)) {
    res = n;
    return;
  }
  if (res <= n)
    return;

  int n1[16][16];
  int n2[16][16];

  next(s, n1, n2);
  dfs(n1, n + 1);
  dfs(n2, n + 1);

  return;
}

int main() {
  while (scanf("%d%d", &X, &Y) && X + Y) {
    int s[16][16];
    getchar();
    rep(i, Y) rep(j, X) {
      char c = getchar();
      switch (c) {
      case 'R':
        s[i][j] = 0;
        break;
      case 'G':
        s[i][j] = 1;
        break;
      case 'B':
        s[i][j] = 2;
        break;
      }
      getchar();
    }

    res = 18;
    dfs(s, 0);
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int X, Y;
int s[128][128];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int res;

void filling(int y, int x, int from, int to) {
  s[y][x] = to;
  rep(d, 4) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= Y || nx < 0 || nx >= X || s[ny][nx] != from)
      continue;
    filling(ny, nx, from, to);
  }
  return;
}

int check() {
  int p = s[0][0];
  rep(i, Y) rep(j, X) {
    if (s[i][j] != p)
      return 0;
  }
  return 1;
}

void dfs(int n) {
  //  printf("%d\\n", n);
  if (n > 16)
    return;
  if (res <= n)
    return;
  //  printf("%d %d\\n", res, n);
  rep(i, 3) {
    if (s[0][0] == i)
      continue;
    int tmp[128][128];
    memcpy(tmp, s, sizeof(s));
    filling(0, 0, s[0][0], i);
    /*    rep(j, Y){
      rep(k, X) printf("%d ", s[j][k]); puts("");
      }puts("");*/
    if (check()) {
      //      puts("OK");
      res = n;
      return;
    }
    dfs(n + 1);
    memcpy(s, tmp, sizeof(s));
  }
  return;
}

int main() {
  while (scanf("%d%d", &X, &Y) && X + Y) {
    getchar();
    rep(i, Y) rep(j, X) {
      //      printf("%d %d\\n", i, j);
      char c = getchar();
      switch (c) {
      case 'R':
        s[i][j] = 0;
        break;
      case 'G':
        s[i][j] = 1;
        break;
      case 'B':
        s[i][j] = 2;
        break;
      }
      getchar();
    }

    res = INF;
    //    puts("in");
    if (check()) {
      puts("0");
    } else {
      dfs(0);
      printf("%d\\n", res + 1);
    }
  }
  return 0;
}