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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 4;
string color = "RGB";
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int w, h;

map<vector<string>, int> memo;

int solve(const vector<string> &s, int prevNum, int d) {
  if (d > 20)
    return INF;
  if (memo.find(s) != memo.end())
    return memo[s];

  int ret = INF;
  for (int i = 0; i < 3; ++i) {
    if (s[0][0] == color[i])
      continue;

    char c = s[0][0];
    vector<string> t = s;
    t[0][0] = color[i];
    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));
    int n = 0;
    while (!q.empty()) {
      int y = q.front().first;
      int x = q.front().second;
      q.pop();
      ++n;

      for (int j = 0; j < 4; ++j) {
        int y2 = y + dy[j];
        int x2 = x + dx[j];
        if (y2 < 0 || y2 >= h || x2 < 0 || x2 >= w)
          continue;
        if (t[y2][x2] == c) {
          t[y2][x2] = color[i];
          q.push(make_pair(y2, x2));
        }
      }
    }

    if (n == prevNum)
      return memo[s] = INF;
    if (n == h * w)
      return memo[s] = 0;
    ret = min(ret, solve(t, n, d + 1) + 1);
  }

  return memo[s] = ret;
}

int main() {
  for (;;) {
    cin >> w >> h;
    if (w == 0)
      return 0;

    vector<string> s(h, string(w, ' '));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> s[i][j];
      }
    }

    memo.clear();
    cout << solve(s, -1, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 4;
string color = "RGB";
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int w, h;

int solve(const vector<string> &s, int prevNum, int d) {
  if (d > 19)
    return INF;

  int ret = INF;

  for (int i = 0; i < 3; ++i) {
    if (s[0][0] == color[i])
      continue;

    char c = s[0][0];
    vector<string> t = s;
    t[0][0] = color[i];
    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));
    int n = 0;
    while (!q.empty()) {
      int y = q.front().first;
      int x = q.front().second;
      q.pop();
      ++n;

      for (int j = 0; j < 4; ++j) {
        int y2 = y + dy[j];
        int x2 = x + dx[j];
        if (y2 < 0 || y2 >= h || x2 < 0 || x2 >= w)
          continue;
        if (t[y2][x2] == c) {
          t[y2][x2] = color[i];
          q.push(make_pair(y2, x2));
        }
      }
    }

    if (n == prevNum)
      return INF;
    if (n == h * w)
      return 0;
    ret = min(ret, solve(t, n, d + 1) + 1);
  }

  return ret;
}

int main() {
  for (;;) {
    cin >> w >> h;
    if (w == 0)
      return 0;

    vector<string> s(h, string(w, ' '));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> s[i][j];
      }
    }

    cout << solve(s, -1, 0) << endl;
  }
}