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

#include <bits/stdc++.h>
using namespace std;

char fie[11][11];
bool used[11][11];
int X, Y;
inline bool check() {
  char c = fie[0][0];
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      if (fie[j][i] != c)
        return false;
  return true;
}

inline void temp(int (*tmp)[11]) {
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      tmp[j][i] = fie[j][i];
}
inline void rev(int (*tmp)[11]) {
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      fie[j][i] = tmp[j][i];
}

int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};

char dc[] = "RGB";

inline bool zch(int x, int y) {
  return x < 0 || y < 0 || x >= X || y >= Y || used[x][y];
}
inline bool ume(int x, int y, char c) {
  if (used[x][y])
    return false;
  used[x][y] = true;
  bool f = false;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (zch(nx, ny))
      continue;
    if (fie[nx][ny] == fie[x][y])
      f |= ume(nx, ny, c);
    else if (fie[nx][ny] == c)
      f = true;
  }
  fie[x][y] = c;
  return f;
}

inline void view() {
  cout << "view" << endl;
  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < X; j++)
      cout << fie[j][i];
    cout << endl;
  }
}

int ans;
inline int solve(int res) {
  if (ans == res)
    return 0;
  if (check()) {
    ans = res;
    return 0;
  }
  char c = fie[0][0];
  int tmp[11][11];
  temp(tmp);
  for (int i = 0; i < 3; i++) {
    if (dc[i] == c)
      continue;
    memset(used, 0, sizeof(used));
    if (ume(0, 0, dc[i]))
      solve(res + 1);
    rev(tmp);
  }
  return res;
}

int main() {
  while (cin >> X >> Y && (X | Y)) {
    for (int i = 0; i < Y; i++)
      for (int j = 0; j < X; j++)
        cin >> fie[j][i];
    ans = 50;
    solve(0);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

char fie[11][11];
bool used[11][11];
int X, Y;
bool check() {
  char c = fie[0][0];
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      if (fie[j][i] != c)
        return false;
  return true;
}

void temp(int (*tmp)[11]) {
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      tmp[j][i] = fie[j][i];
}
void rev(int (*tmp)[11]) {
  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
      fie[j][i] = tmp[j][i];
}

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

char dc[] = "RGB";

bool zch(int x, int y) {
  return x < 0 || y < 0 || x >= X || y >= Y || used[x][y];
}
bool ume(int x, int y, char c) {
  if (used[x][y])
    return false;
  used[x][y] = true;
  bool f = false;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (zch(nx, ny))
      continue;
    if (fie[nx][ny] == fie[x][y])
      f |= ume(nx, ny, c);
    else if (fie[nx][ny] == c)
      f = true;
  }
  fie[x][y] = c;
  return f;
}

void view() {
  cout << "view" << endl;
  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < X; j++)
      cout << fie[j][i];
    cout << endl;
  }
}

int solve() {
  if (check())
    return 0;
  char c = fie[0][0];
  int tmp[11][11];
  temp(tmp);
  int res = 50;
  for (int i = 0; i < 3; i++) {
    if (dc[i] == c)
      continue;
    memset(used, 0, sizeof(used));
    if (ume(0, 0, dc[i]))
      res = min(res, solve() + 1);
    rev(tmp);
  }
  return res;
}

int main() {
  while (cin >> X >> Y && (X | Y)) {
    for (int i = 0; i < Y; i++)
      for (int j = 0; j < X; j++)
        cin >> fie[j][i];
    cout << solve() << endl;
  }
}