Problem p00606 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};

signed main() {
  cout << fixed << setprecision(8);
  for (int n; cin >> n && n;) {
    char s, t, b;
    cin >> s >> t >> b;
    double f[3][3] = {};
    double g[3][3] = {};
    pii S, T, B;
    S = pii((s - 'A') / 3, (s - 'A') % 3);
    T = pii((t - 'A') / 3, (t - 'A') % 3);
    B = pii((b - 'A') / 3, (b - 'A') % 3);
    f[S.first][S.second] = 1;
    while (n--) {
      rep(i, 0, 3) rep(j, 0, 3) g[i][j] = 0;
      swap(f, g);
      rep(a, 0, 3) rep(b, 0, 3) {
        int cnt = 0;
        rep(i, 0, 4) {
          if (a + di[i] == B.first && b + dj[i] == B.second)
            continue;
          if (0 > a + di[i] || a + di[i] > 2)
            continue;
          if (0 > b + dj[i] || b + dj[i] > 2)
            continue;
          f[a + di[i]][b + dj[i]] += g[a][b] / 4;
          cnt++;
        }
        f[a][b] += (g[a][b] / 4) * (4 - cnt);
      }
    }
    cout << f[T.first][T.second] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};

signed main() {
  cout << fixed << setprecision(8);
  for (int n; cin >> n && n;) {
    char s, t, b;
    cin >> s >> t >> b;
    double f[3][3] = {};
    double g[3][3] = {};
    pii S, T, B;
    S = pii((s - 'A') / 3, (s - 'A') % 3);
    T = pii((t - 'A') / 3, (t - 'A') % 3);
    B = pii((b - 'A') / 3, (b - 'A') % 3);
    f[S.first][S.second] = 1;
    while (n--) {
      rep(i, 0, 3) rep(j, 0, 3) g[i][j] = 0;
      swap(f, g);
      rep(a, 0, 3) rep(b, 0, 3) {
        int cnt = 0;
        rep(i, 0, 4) {
          if (a + di[i] == B.first && b + dj[i] == B.second)
            continue;
          if (0 > a + di[i] && a + di[i] > 2)
            continue;
          if (0 > b + dj[i] && b + dj[i] > 2)
            continue;
          f[a + di[i]][b + dj[i]] += g[a][b] / 4;
          cnt++;
        }
        f[a][b] += (g[a][b] / 4) * (4 - cnt);
      }
    }
    cout << f[T.first][T.second] << endl;
  }
  return 0;
}