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

#include <iostream>
#include <queue>
#include <utility>
using namespace std;

int main() {
  int x, y;

  while (cin >> x >> y && x) {
    string m;
    for (int i = 0; i < y; i++) {
      for (int l = 0; l < x; l++) {
        string k;
        cin >> k;
        m += k;
      }
    }
    typedef pair<string, int> psi;
    queue<psi> que;
    que.push(psi(m, 0));
    while (!que.empty()) {
      psi now = que.front();
      que.pop();

      {
        bool ok = true;
        for (int i = 0; i < now.first.size(); i++)
          if (now.first[i] != now.first[0])
            ok = false;
        if (ok) {
          cout << now.second << endl;
          break;
        }
      }

      {
        char s[4] = "RGB";
        for (int c = 0; c < 3; c++) {
          if (now.first[0] == s[c])
            continue;
          char b[20][20] = {};
          for (int i = 1; i <= y; i++) {
            for (int j = 1; j <= x; j++) {
              b[i][j] = now.first[(i - 1) * x + (j - 1)];
            }
          }
          typedef pair<int, int> pii;
          queue<pii> Q;
          Q.push(pii(1, 1));
          int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
          char ori = b[1][1];
          b[1][1] = s[c];
          while (!Q.empty()) {
            pii g = Q.front();
            Q.pop();

            for (int i = 0; i < 4; i++) {
              if (b[g.first + dy[i]][g.second + dx[i]] == ori) {
                b[g.first + dy[i]][g.second + dx[i]] = s[c];
                pii next;
                next.first = g.first + dy[i];
                next.second = g.second + dx[i];
                Q.push(next);
              }
            }
          }

          psi next;
          next.second = now.second + 1;
          for (int i = 1; i <= y; i++) {
            for (int j = 1; j <= x; j++) {
              next.first += b[i][j];
            }
          }

          que.push(next);
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define M_PI 3.141592
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define pri_max 60000
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;

typedef pair<int, int> pii;
int x, y;
char a[20][20];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
map<string, int> m;

int dfs(int z) {
  bool ok = true;
  string tmp;
  char b[20][20];

  for (int i = 1; i <= y; i++) {
    for (int j = 1; j <= x; j++) {
      if (a[1][1] != a[i][j])
        ok = false;
      tmp += a[i][j];
      b[i][j] = a[i][j];
    }
  }
  if (ok)
    return z;
  if (!m.count(tmp))
    m[tmp] = z;
  else if (m[tmp] <= z)
    return INT_MAX;

  char ord[4] = "RGB";
  int ans = INT_MAX;
  for (int i = 0; i < 3; i++) {
    char o, t;
    o = a[1][1];
    t = ord[i];
    if (o == t)
      continue;
    a[1][1] = t;
    queue<pii> que;
    que.push(pii(1, 1));
    while (!que.empty()) {
      pii now = que.front();
      que.pop();

      for (int l = 0; l < 4; l++) {
        pii next;
        next.first = now.first + dx[l];
        next.second = now.second + dy[l];

        if (a[next.first][next.second] == o) {
          a[next.first][next.second] = t;
          que.push(next);
        }
      }
    }

    ans = min(ans, dfs(z + 1));

    for (int l = 1; l <= y; l++) {
      for (int k = 1; k <= x; k++) {
        a[l][k] = b[l][k];
      }
    }
  }

  return ans;
}

int main() {

  while (cin >> x >> y && x && y) {
    memset(a, 0, sizeof a);
    m.clear();
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> a[i][j];
      }
    }
    cout << dfs(0) << endl;
  }
}