Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
namespace Base {
#define mr make_pair
typedef long long ll;
typedef double db;
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const ll infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
template <typename T> void read(T &x) {
  x = 0;
  int fh = 1;
  double num = 1.0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (ch == '.') {
    ch = getchar();
    while (isdigit(ch)) {
      num /= 10;
      x = x + num * (ch - '0');
      ch = getchar();
    }
  }
  x = x * fh;
}
template <typename T> void chmax(T &x, T y) { x = x < y ? y : x; }
template <typename T> void chmin(T &x, T y) { x = x > y ? y : x; }
} // namespace Base
using namespace Base;

const int N = 210;
int n, cnt[4], q1[N * N * N], q2[N * N * N], q3[N * N * N];
bool mp[N][N][N];
double g[N][N][N], f[N][N][N];
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    int x;
    read(x);
    cnt[x]++;
  }
  mp[cnt[1]][cnt[2]][cnt[3]] = true;
  g[cnt[1]][cnt[2]][cnt[3]] = 1;
  q1[1] = cnt[1], q2[1] = cnt[2], q3[1] = cnt[3];
  int pl = 1, pr = 1;
  while (pl <= pr) {
    int x = q1[pl], y = q2[pl], z = q3[pl], nx, ny, nz;
    f[x][y][z] /= g[x][y][z];
    pl++;
    double t = 1.0 * n / (x + y + z);
    if (x > 0) {
      nx = x - 1, ny = y, nz = z;
      if (!mp[nx][ny][nz]) {
        pr++;
        q1[pr] = nx, q2[pr] = ny, q3[pr] = nz;
        mp[nx][ny][nz] = true;
      }
      double p = 1.0 * x / (x + y + z);
      g[nx][ny][nz] += p * g[x][y][z];
      f[nx][ny][nz] += p * g[x][y][z] * (f[x][y][z] + t);
    }
    if (y > 0) {
      nx = x + 1, ny = y - 1, nz = z;
      if (!mp[nx][ny][nz]) {
        pr++;
        q1[pr] = nx, q2[pr] = ny, q3[pr] = nz;
        mp[nx][ny][nz] = true;
      }
      double p = 1.0 * y / (x + y + z);
      g[nx][ny][nz] += p * g[x][y][z];
      f[nx][ny][nz] += p * g[x][y][z] * (f[x][y][z] + t);
    }

    if (z > 0) {
      nx = x, ny = y + 1, nz = z - 1;
      if (!mp[nx][ny][nz]) {
        pr++;
        q1[pr] = nx, q2[pr] = ny, q3[pr] = nz;
        mp[nx][ny][nz] = true;
      }
      double p = 1.0 * z / (x + y + z);
      g[nx][ny][nz] += p * g[x][y][z];
      f[nx][ny][nz] += p * g[x][y][z] * (f[x][y][z] + t);
    }
  }
  printf("%.10lf\n", f[0][0][0]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
namespace Base {
#define mr make_pair
typedef long long ll;
typedef double db;
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const ll infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
template <typename T> void read(T &x) {
  x = 0;
  int fh = 1;
  double num = 1.0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (ch == '.') {
    ch = getchar();
    while (isdigit(ch)) {
      num /= 10;
      x = x + num * (ch - '0');
      ch = getchar();
    }
  }
  x = x * fh;
}
template <typename T> void chmax(T &x, T y) { x = x < y ? y : x; }
template <typename T> void chmin(T &x, T y) { x = x > y ? y : x; }
} // namespace Base
using namespace Base;

const int N = 310;
int n, cnt[4], q1[N * N * N], q2[N * N * N], q3[N * N * N];
bool mp[N][N][N];
double g[N][N][N], f[N][N][N];
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    int x;
    read(x);
    cnt[x]++;
  }
  mp[cnt[1]][cnt[2]][cnt[3]] = true;
  g[cnt[1]][cnt[2]][cnt[3]] = 1;
  q1[1] = cnt[1], q2[1] = cnt[2], q3[1] = cnt[3];
  int pl = 1, pr = 1;
  while (pl <= pr) {
    int x = q1[pl], y = q2[pl], z = q3[pl], nx, ny, nz;
    f[x][y][z] /= g[x][y][z];
    pl++;
    double t = 1.0 * n / (x + y + z);
    if (x > 0) {
      nx = x - 1, ny = y, nz = z;
      if (!mp[nx][ny][nz]) {
        pr++;
        q1[pr] = nx, q2[pr] = ny, q3[pr] = nz;
        mp[nx][ny][nz] = true;
      }
      double p = 1.0 * x / (x + y + z);
      g[nx][ny][nz] += p * g[x][y][z];
      f[nx][ny][nz] += p * g[x][y][z] * (f[x][y][z] + t);
    }
    if (y > 0) {
      nx = x + 1, ny = y - 1, nz = z;
      if (!mp[nx][ny][nz]) {
        pr++;
        q1[pr] = nx, q2[pr] = ny, q3[pr] = nz;
        mp[nx][ny][nz] = true;
      }
      double p = 1.0 * y / (x + y + z);
      g[nx][ny][nz] += p * g[x][y][z];
      f[nx][ny][nz] += p * g[x][y][z] * (f[x][y][z] + t);
    }

    if (z > 0) {
      nx = x, ny = y + 1, nz = z - 1;
      if (!mp[nx][ny][nz]) {
        pr++;
        q1[pr] = nx, q2[pr] = ny, q3[pr] = nz;
        mp[nx][ny][nz] = true;
      }
      double p = 1.0 * z / (x + y + z);
      g[nx][ny][nz] += p * g[x][y][z];
      f[nx][ny][nz] += p * g[x][y][z] * (f[x][y][z] + t);
    }
  }
  printf("%.10lf\n", f[0][0][0]);
  return 0;
}