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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  bool hasNext() { return !cin.eof(); }
};
Input in;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

bool isOutofRange(int x, int y, int b) {
  return x < 0 || x >= 3 || y < 0 || y >= 3 || y * 3 + x == b;
}

int dp[20][3][3];

int main() {
  while (in.hasNext()) {
    int n = in.nextInt();
    if (n == 0)
      break;
    rep(i, 20) rep(j, 3) rep(k, 3) dp[i][j][k] = 0;
    int s = in.next()[0] - 'A', t = in.next()[0] - 'A', b = in.next()[0] - 'A';
    // cout<<s<<" "<<t<<" "<<b<<endl;
    // int x = s % 3, y = s / 3;
    dp[0][s / 3][s % 3] = 1;

    rep(i, n) {
      rep(x, 3) rep(y, 3) rep(j, 4) {
        int nx = x + dx[j], ny = y + dy[j];
        if (isOutofRange(nx, ny, b))
          dp[i + 1][y][x] += dp[i][y][x];
        else
          dp[i + 1][ny][nx] += dp[i][y][x];
      }
    }

    ll ans = 0LL;
    rep(i, 3) rep(j, 3) ans += dp[n][i][j];

    printf("%.15lf\\n", (double)dp[n][t / 3][t % 3] / ans);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

pii f(char c) {
  pii ret;
  int a = c - 'A';
  ret.fi = a / 3;
  ret.se = a % 3;
  return ret;
}

int main() {
  int n, bunshi, bunbo;
  char a, b, c;
  pii s, t, m;
  pip p;
  while (cin >> n && n > 0) {
    cin >> a >> b >> c;
    s = f(a);
    t = f(b);
    m = f(c);
    bunshi = 0;
    bunbo = 0;
    queue<pip> q;
    q.push(pip(n, s));
    while (!q.empty()) {
      p = q.front();
      q.pop();
      if (p.fi == 0) {
        bunbo++;
        if (p.se == t)
          bunshi++;
        continue;
      }
      for (int i = 0; i < 4; i++) {
        pii next(p.se.fi + dx[i], p.se.se + dy[i]);
        if (next.fi < 0 || next.fi > 2 || next.se < 0 || next.se > 2 ||
            next == m)
          q.push(pip(p.fi - 1, p.se));
        else
          q.push(pip(p.fi - 1, next));
      }
    }
    printf("%.15lf\\n", (double)bunshi / bunbo);
  }
}