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

#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
using namespace std;

struct state {
  int a[9];
  int num;
};

int count(int a[9]) {
  deque<state> q;
  state tmp, work;
  int ret = 0, tmptimes;
  for (int i = 0; i < 9; i++)
    tmp.a[i] = a[i];
  tmp.num = 0;
  q.push_back(tmp);
  while (!q.empty()) {
    tmp = q.front();
    ret = max(ret, tmp.num);
    tmptimes = tmp.num;
    for (int i = 0; i < 9; i++) {
      work.a[i] = tmp.a[i];
      // cout << work.a[i];
    }
    // cout << endl;
    q.pop_front();
    for (int i = 0; i <= 6; i++) {
      for (int j = 0; j < 9; j++)
        work.a[j] = tmp.a[j];
      work.num = tmp.num;
      if (work.a[i] && work.a[i + 1] && work.a[i + 2]) {
        work.a[i]--;
        work.a[i + 1]--;
        work.a[i + 2]--;
        work.num++;
        q.push_back(work);
      }
    }
    for (int i = 0; i <= 8; i++) {
      for (int j = 0; j < 9; j++)
        work.a[j] = tmp.a[j];
      work.num = tmp.num;
      if (work.a[i] >= 3) {
        work.a[i] -= 3;
        work.num++;
        q.push_back(work);
      }
    }
  }
  return ret;
}

int main(void) {
  int n;

  cin >> n;
  while (n--) {
    int R[9], G[9], B[9];
    int num[9];
    char color[9];
    memset(R, 0, sizeof(R));
    memset(G, 0, sizeof(G));
    memset(B, 0, sizeof(B));

    for (int i = 0; i < 9; i++)
      cin >> num[i];
    for (int i = 0; i < 9; i++)
      cin >> color[i];
    for (int i = 0; i < 9; i++) {
      if (color[i] == 'R') {
        R[num[i] - 1]++;
      } else if (color[i] == 'G') {
        G[num[i] - 1]++;
      } else {
        B[num[i] - 1]++;
      }
    }
    if (count(R) + count(G) + count(B) >= 3) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
using namespace std;

struct state {
  int a[9];
  int num;
};
int count(int a[9]) {
  deque<state> q;
  state tmp, work;
  int ret = 0, tmptimes;
  for (int i = 0; i < 9; i++)
    tmp.a[i] = a[i];
  tmp.num = 0;
  q.push_back(tmp);
  while (!q.empty()) {
    tmp = q.front();
    ret = max(ret, tmp.num);
    tmptimes = tmp.num;
    for (int i = 0; i < 9; i++) {
      work.a[i] = tmp.a[i];
      // cout << work.a[i];
    }
    // cout << endl;
    q.pop_front();
    for (int i = 0; i <= 7; i++) {
      for (int j = 0; j < 9; j++)
        work.a[j] = tmp.a[j];
      work.num = tmp.num;
      if (work.a[i] && work.a[i + 1] && work.a[i + 2]) {
        work.a[i]--;
        work.a[i + 1]--;
        work.a[i + 2]--;
        work.num++;
        q.push_back(work);
      }
    }
    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j < 9; j++)
        work.a[j] = tmp.a[j];
      work.num = tmp.num;
      if (work.a[i] >= 3) {
        work.a[i] -= 3;
        work.num++;
        q.push_back(work);
      }
    }
  }
  return ret;
}

int main(void) {
  int n;

  cin >> n;
  while (n--) {
    int R[9], G[9], B[9];
    int num[9];
    char color[9];
    memset(R, 0, sizeof(R));
    memset(G, 0, sizeof(G));
    memset(B, 0, sizeof(B));

    for (int i = 0; i < 9; i++)
      cin >> num[i];
    for (int i = 0; i < 9; i++)
      cin >> color[i];
    for (int i = 0; i < 9; i++) {
      if (color[i] == 'R') {
        R[num[i] - 1]++;
      } else if (color[i] == 'G') {
        G[num[i] - 1]++;
      } else {
        B[num[i] - 1]++;
      }
    }
    if (count(R) + count(G) + count(B) >= 3) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}